jQuery不能与 XML标记一起使用

时间:2023-01-19 21:48:20

I was using jQuery doing some XML work. Then jQuery told me it can't find the <col> tag giving me empty data. After having a talk with jQuery, seems like it just doesn't like to work with <col> XML tags, maybe some expert can explain this to me?

我正在使用jQuery做一些XML工作。然后jQuery告诉我它找不到给我空数据的标签。在与jQuery讨论之后,似乎只是不喜欢使用 XML标签,也许有些专家可以向我解释一下这个问题?

Here's my 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>

Here's my code for it:

这是我的代码:

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());
});

as you can see my first output is

你可以看到我的第一个输出是

col - 1 col - 2 col - 3 col - 4 col - 5 

then I found out jQuery doesn't like <col> tag, I changed it to < colm >< /colm > instead, and it gives me this:

然后我发现jQuery不喜欢标签,我改为 ,它给了我这个:

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

which is what I want.

这就是我想要的。

How can I make my jQuery love the <col> tag?

如何让我的jQuery喜欢标签?

1 个解决方案

#1


3  

I don't claim to be an expert, but <col> exists in HTML as an empty element. You can't give that element text, even if you try to use it as an XML element, because jQuery would be breaking the HTML DOM rules otherwise.

我并不认为自己是专家,但作为空元素存在于HTML中。即使您尝试将其用作XML元素,也不能给该元素文本,因为jQuery会打破HTML DOM规则。

Since jQuery was made to work with HTML rather than XML, I can't think of a good workaround besides simply not using the name <col>...

由于jQuery是用HTML而不是XML编写的,除了不使用名称之外,我想不出一个好的解决方法......

#1


3  

I don't claim to be an expert, but <col> exists in HTML as an empty element. You can't give that element text, even if you try to use it as an XML element, because jQuery would be breaking the HTML DOM rules otherwise.

我并不认为自己是专家,但作为空元素存在于HTML中。即使您尝试将其用作XML元素,也不能给该元素文本,因为jQuery会打破HTML DOM规则。

Since jQuery was made to work with HTML rather than XML, I can't think of a good workaround besides simply not using the name <col>...

由于jQuery是用HTML而不是XML编写的,除了不使用名称之外,我想不出一个好的解决方法......