jQuery 不能与 一起使用XML标签

发布于 2024-12-14 21:52:59 字数 2460 浏览 0 评论 0 原文

我正在使用 jQuery 做一些 XML 工作。然后 jQuery 告诉我它找不到给我空数据的 标签。在与 jQuery 交谈之后,似乎它只是不喜欢使用 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 不喜欢 标签,我将其更改为 <科尔姆><相反, /colm > ,它给了我这个:

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

这就是我想要的。

如何让我的 jQuery 喜欢 标签?

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?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若能看破又如何 2024-12-21 21:52:59

我并不声称自己是专家,但是 < /code> 在 HTML 中作为空元素存在。即使您尝试将其用作 XML 元素,您也不能提供该元素文本,因为否则 jQuery 将违反 HTML DOM 规则。

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

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.

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>...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文