【发布时间】:2011-11-11 08:07:04
【问题描述】:
我正在使用 jQuery 做一些 XML 工作。然后jQuery告诉我它找不到<col>标签给我空数据。与 jQuery 交谈后,似乎它只是不喜欢使用<col> XML 标签,也许有专家可以向我解释一下?
这是我的 XML:
<field>
<property>
<label>Matrix Question</label>
<rows>
<row>row - 1a</row>
<row>row - 2a</row>
<row>row - 3a</row>
<row>row - 4a</row>
</rows>
<cols>
<col>col - 1</col>
<col>col - 2</col>
<col>col - 3</col>
<col>col - 4</col>
<col>col - 5</col>
</cols>
<isrequired>true</isrequired>
</property>
这是我的代码:
var xmlWithCol = "<field> <property> <label>Matrix Question</label> <rows> <row>row - 1a</row> <row>row - 2a</row> <row>row - 3a</row> <row>row - 4a</row> </rows> <cols> <col>col - 1</col> <col>col - 2</col> <col>col - 3</col> <col>col - 4</col> <col>col - 5</col> </cols> <isrequired>true</isrequired> </property> </field>";
var xmlWithoutCol = "<field> <property> <label>Matrix Question</label> <rows> <row>row - 1a</row> <row>row - 2a</row> <row>row - 3a</row> <row>row - 4a</row> </rows> <cols> <colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm> </cols> <isrequired>true</isrequired> </property> </field>"
$(xmlWithCol).find("cols").each(function ()
{
alert($(this).html());
});
$(xmlWithoutCol).find("cols").each(function ()
{
alert($(this).html());
});
你可以看到我的第一个输出是
col - 1 col - 2 col - 3 col - 4 col - 5
然后我发现 jQuery 不喜欢 <col> 标签,我将它改为 < colm >< /colm >,它给了我这个:
<colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm>
这就是我想要的。
如何让我的 jQuery 喜欢 <col> 标签?
【问题讨论】:
-
你为什么写
cols?在您的发现中,尝试像这样删除最后一个 s.find('col') -
通常你对 xml 文档使用 $.parseXML(xmlString).find("cols") 而不是 $(xmlString).find("cols"),那么 jQuery 也会正确处理它。