【问题标题】:jQuery doesn't work with <col> XML tagsjQuery 不适用于 <col> XML 标签
【发布时间】:2011-11-11 08:07:04
【问题描述】:

我正在使用 jQuery 做一些 XML 工作。然后jQuery告诉我它找不到&lt;col&gt;标签给我空数据。与 jQuery 交谈后,似乎它只是不喜欢使用&lt;col&gt; 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 不喜欢 &lt;col&gt; 标签,我将它改为 &lt; colm &gt;&lt; /colm &gt;,它给了我这个:

<colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm> 

这就是我想要的。

如何让我的 jQuery 喜欢 &lt;col&gt; 标签?

【问题讨论】:

  • 你为什么写cols?在您的发现中,尝试像这样删除最后一个 s .find('col')
  • 通常你对 xml 文档使用 $.parseXML(xmlString).find("cols") 而不是 $(xmlString).find("cols"),那么 jQuery 也会正确处理它。

标签: jquery xml


【解决方案1】:

我不自称是专家,但&lt;col&gt; exists in HTML as an empty element。即使您尝试将其用作 XML 元素,也不能提供该元素文本,否则 jQuery 会破坏 HTML DOM 规则。

由于 jQuery 是用于 HTML 而不是 XML,我想不出一个好的解决方法,除了不使用名称 &lt;col&gt;...

【讨论】:

  • 谢谢你的课,我想我得告诉“忍住!” =P
猜你喜欢
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
  • 1970-01-01
  • 2020-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多