我正在使用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>
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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());
});
Run Code Online (Sandbox Code Playgroud)
你可以看到我的第一个输出是
col - 1 col - 2 col - 3 col - 4 col - 5
Run Code Online (Sandbox Code Playgroud)
然后我发现jQuery不喜欢<col>标签,我把它改成了< colm >< /colm >,它给了我这个:
<colm>col - 1</colm> <colm>col - 2</colm> <colm>col - 3</colm> <colm>col - 4</colm> <colm>col - 5</colm>
Run Code Online (Sandbox Code Playgroud)
这就是我想要的.
如何让我的jQuery喜欢这个<col>标签?
我并不声称自己是专家,但它<col>作为空元素存在于 HTML 中。即使您尝试将其用作 XML 元素,您也不能提供该元素文本,因为否则 jQuery 将违反 HTML DOM 规则。
由于 jQuery 是为 HTML 而不是 XML 设计的,除了简单地不使用名称之外,我想不出一个好的解决方法<col>......