【问题标题】:jQuery escaping <style> contentjQuery 转义 <style> 内容
【发布时间】:2013-02-26 20:09:48
【问题描述】:

我使用 jquery 作为 xml 解析器(大错误)。但是,我现在投入太多,无法转换。我遇到了 jQuery 自动转义 &lt;style&gt; 标签内容的问题。

var a = $("<style><foo>content</foo></style>"),
    b = $("<bar><foo>content</foo></bar>");

b.find('foo').length // => 1
a.find('foo').length // => 0

b.html() // => '<foo>content</foo>'
a.html() // => 'foo>content</foo>'
         //     ^--- missing '<'

b.text() // 'test'
a.text() // 'foo>content</foo>'

有没有办法阻止 jQuery 这样做?

【问题讨论】:

    标签: javascript jquery xml


    【解决方案1】:

    你可以使用:

    var xml = jQuery.parseXML("<style><foo>content</foo></style>");
    var a = $(xml);
    

    为了防止jQuery对style标签区别对待。

    这是一个演示:http://jsfiddle.net/FABmh/

    【讨论】:

    • 啊。我在节点上使用 jquery,parseXML 在提供 XML 片段时抛出异常。虽然相同的代码在浏览器中工作。谢谢!
    • @iliacholy 抱歉发了双重信息,但您遇到了什么异常?
    • this => stackoverflow.com/questions/11696853/… 我没有使用任何框架。
    • @iliacholy 是的,当然。您是否使用任何可以解析 xml 的模块?您可以尝试包含 this module 之类的内容。
    • 不,但不难找到。由于可移植性,我只是想使用 jquery。
    猜你喜欢
    • 1970-01-01
    • 2013-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多