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值?

假设<rect .../>标记 xml 的一部分,那么如何使用 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元素中添加一个属性并使用该类选择它们。我添加了rect元素。检查这个小提琴class="sample"

 $('.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-label. 如果要使用任何自定义属性,则需要将它们与data-前缀一起使用。
此外,id属性的第一个字符必须是字母,例如a17not 17
如果您考虑以上两个,那么要访问此属性,您可以这样做

$('#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 回答