gpt4 book ai didi

jquery - 使用Jquery解析xml时如何保留html

转载 作者:数据小太阳 更新时间:2023-10-29 02:21:33 26 4
gpt4 key购买 nike

假设我有以下 XML 结构:

<root>
<item>
<item1>some text is <b>here</b></item1>
<item2>more text is here</item2>
</item>
</root>

我可以使用类似的方法解析 xml:

$(responseXML).find('item').each(function(){
var first_text = $(this).find('item1').text();
var second_text = $(this).find('item2').text();
}

但是使用 .text() 时不会保留 html。 html() 不适用于 jquery 中的 xml。

关于在解析 xml 时如何保留内部 html 的任何想法?

最佳答案

将您不想解释为 xml 的部分嵌套在 CDATA 部分中例如

<root>
<item>
<item1><![CDATA[some text is <b>here</b>]]></item1>
<item2><![CDATA[more text is here]]></item2>
</item>
</root>

编辑:请注意,如果使用 .text() 不起作用,请尝试以下操作:

引自:http://dev.jquery.com/ticket/2425

I've just re-encountered this. For the curious my workaround is instead of using .text() to use this plugin I created (simply replace .text() with .getCDATA():

jQuery.fn.getCDATA = function() {
if($.browser.msie)
return this[0].childNodes[0].nodeValue;
// Other browsers do this
return this[0].childNodes[1].nodeValue;
};

It ain't pretty, but in my case did the job.

关于jquery - 使用Jquery解析xml时如何保留html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/937911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com