0

この形式の json 要素があります。

<rect style="fill: #888888; display: inline;" id="17" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A18"></rect>

label属性値を取得するにはどうすればよいですか?

xml<rect .../>の一部にタグを付けるとしたら、C# コンソール アプリケーションを使用して同じものを取得するにはどうすればよいでしょうか?

4

3 に答える 3

2

これを試して:

var elem = document.getElementsById('17');
var label = elem.getAttribute('label');
alert(label);

jQuery の使用:

alert($('#17').attr('label'));

次のような 300 個の要素があります。

次にこれを試してください:

$('rect').each(function(){
     alert($(this).attr('label'));
});

ここにデモがあります

これを行う別の方法として、クラス属性をrect要素に追加し、そのクラスを使用してそれらを選択します。class="sample" rect要素を追加しました。このフィドルをチェック

 $('.sample').each(function(){
     alert($(this).attr('label'));
 });

サンプル xml ファイル。

 <?xml version="1.0" encoding="utf-8" ?>
 <Test>
      <rect style="fill: #888888; display: inline;" id="17" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A18"></rect>
      <rect style="fill: #888888; display: inline;" id="18" width="35.823246" height="35.823246" x="456.61066" y="65.9505" class="seatObj" label="A19"></rect>
 </Test>

C# コンソール アプリケーションを使用した xml の解析:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace Sample
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load("Url for Sample.xml");

            XmlNodeList elemList = doc.GetElementsByTagName("rect");
            for (int i = 0; i < elemList.Count; i++)
            {
                string attrVal = elemList[i].Attributes["label"].Value;
                Console.WriteLine(attrVal);
            }
            Console.ReadLine();
        }
    }
}
于 2013-10-23T07:20:41.567 に答える
0

まず、label有効な DOM 属性ではありません。に変更する必要がありますdata-labeldata-カスタム属性を使用する場合は、接頭辞を付けて使用する必要があります。
また、id属性の最初の文字は文字でなければなりません (例:a17ではありません) 17
上記の2つを考慮すると、この属性にアクセスするには、次のことができます

$('#a17').attr('data-label')

またはプレーンJavaScript

document.getElementById('a17').dataSet.label
于 2013-10-23T07:18:16.700 に答える
0

これを試して:

var elem = $('#' + id)
alert(elem.attr("label"));
于 2013-10-23T07:19:15.230 に答える