简体   繁体   English

防止jQuery使用自定义XML更改自动关闭标签

[英]Preventing jQuery from changing self-closing tags with custom XML

I'm creating a browser plugin (bookmarklet) that exacts a large XML document embedded within the page's <textarea> element. 我正在创建一个浏览器插件(书签),该插件可以嵌入页面的<textarea>元素中的大型XML文档。 This is a custom XML language that includes a few self-closing tags (eg, <custom-tag /> ). 这是一种自定义XML语言,其中包含一些自闭标签(例如<custom-tag /> )。 I'm able to capture the XML document, wrap it in a jQuery object variable (eg, $(myXml) ), and easily manipulate the data. 我能够捕获XML文档,将其包装在jQuery对象变量(例如$(myXml) )中,并轻松地操作数据。 However, after I try inserting the manipulated XML document back in the page's <textarea> element, all the self-closing tags are changed into separate opening and closing tags. 但是,当我尝试将操纵过的XML文档重新插入页面的<textarea>元素中之后,所有的自动关闭标签都变成了单独的打开和关闭标签。 Here's an example of what's happening: 这是正在发生的事的一个例子:

BEFORE: 之前:

<outer-tag>
    <SELF-CLOSING-TAG foo="bar" />
    <second-tag>
        <third-tag>Some Value</third-tag>
    </second-tag>
</outer-tag>

AFTER jQUERY MANIPULATION: jQUERY操作之后:

<outer-tag>
    <SELF-CLOSING-TAG foo="bar">
        <second-tag>
            <third-tag>Some Value</third-tag>
        </second-tag>
    </SELF-CLOSING-TAG>
</outer-tag>

Is there any way I can prevent jQuery (or is it just JavaScript or the browser?) from turning these self-closing tags into opening and closing tags? 有什么方法可以防止jQuery(或者仅仅是JavaScript还是浏览器?)将这些自关闭标签转换为打开和关闭标签? This latter way is invalid for the XML schema we're using. 对于我们正在使用的XML模式,后一种方法无效。 If there's no way around that, are there any other hacks worth considering? 如果没有解决办法,还有其他值得考虑的技巧吗?

UPDATE: Drejc's ideas pointed me in the right direction to solve this issue. 更新:Drejc的想法为我指出了解决此问题的正确方向。 After his input, I got the additional advice on this additional SO post and together this solved the problem. 在他的输入之后,我获得了有关此额外SO帖子的其他建议,并共同解决了该问题。

Have you checked when this happens. 您是否检查过这种情况。 After inserting a new element or after read out. 插入新元素后或读出后。 You might give it a try with no self closing elements, like this: 您可以尝试不使用自闭元素的尝试,如下所示:

<outer-tag>
    <SELF-CLOSING-TAG foo="bar"></<SELF-CLOSING-TAG>
    <second-tag>
        <third-tag>Some Value</third-tag>
    </second-tag>
</outer-tag>

It might help. 这可能会有所帮助。

This "feels like" the result of an XSD definition that requires your self-closing tag to wrap around the "second-tag". 这让XSD定义的结果“感觉”起来,该定义要求您的自闭合标签必须包裹“ second-tag”。 Give that a look. 看一下 I'm also suspicious this might not happen if you try a different browser. 我还怀疑,如果您尝试使用其他浏览器,可能不会发生这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM