Javascript jQuery:contains()选择器不工作
我使用jQuery:contains()选择器仅显示XML文件中包含特定文本字符串的项。它在FF、Safari、Chrome等环境中都能完美工作,但在IE7、8或9环境中却不行。有什么想法吗Javascript jQuery:contains()选择器不工作,javascript,jquery,xml,internet-explorer,jquery-selectors,Javascript,Jquery,Xml,Internet Explorer,Jquery Selectors,我使用jQuery:contains()选择器仅显示XML文件中包含特定文本字符串的项。它在FF、Safari、Chrome等环境中都能完美工作,但在IE7、8或9环境中却不行。有什么想法吗 jQuery(RSSqa).find(“项:包含('Q&A with'):lt(1)”).each(函数(){ 这段代码以XML格式显示最新项目,其中包含“Q&a with” 这里是一些RSS提要 <rss version="2.0"> <channel> <title>
jQuery(RSSqa).find(“项:包含('Q&A with'):lt(1)”).each(函数(){
这段代码以XML格式显示最新项目,其中包含“Q&a with”
这里是一些RSS提要
<rss version="2.0">
<channel>
<title>Headlines</title>
<link>Coyotes</link>
<description>Coyotes</description>
<lastBuildDate>Fri, 17 Jun 2011 16:54:44 EDT</lastBuildDate>
<item>
<title>Q & A with Brandon</title>
<enclosure length="63471" type="image/png" url="http://google.com/images/upload/325.jpg"></enclosure><link>http://google.com/news.htm?id=56362</link>
<description>description</description>
<author>Dave</author>
<pubDate>Tue, 24 May 2011 09:58:00 EDT</pubDate>
<guid>http://google.com/news.htm?id=56362</guid>
<comments>Brandon is a winner</comments>
</item>
<item>
<title>Q & A with Jason</title>
<enclosure length="63471" type="image/png" url="http://google.com/images/upload/323.jpg"></enclosure><link>http://google.com/news.htm?id=56363</link>
<description>description</description>
<author>Dave</author>
<pubDate>Tue, 24 May 2011 09:58:00 EDT</pubDate>
<guid>http://google.com/news.htm?id=56363</guid>
<comments>Brandon is a winner</comments>
</item>
</channel>
</rss>
标题
土狼
土狼
2011年6月17日星期五美国东部时间16:54:44
与布兰登的问答
http://google.com/news.htm?id=56362
描述
';
qaData+=''+摘要+'
;
qaData+='';
jQuery('div.DCnarModR').append(jQuery(qaData));
});
});
});
我也看到了这一点。无论出于何种原因,jQuery似乎只会在InternetExplorer中的实际XML对象上执行其标准遍历函数
我使用的解决方法是使用一些简单的特性检测,首先查看您是否在IE中,然后将XML字符串转换为XML对象以供jQuery使用
比如:
function Convert(xmlStr) {
if (window.ActiveXObject && (new window.ActiveXObject("Microsoft.XMLDOM") != null)) {
var xd = new window.ActiveXObject("Microsoft.XMLDOM");
if (xd) {
xd.loadXML(xmlStr);
return xd;
}
}
return xmlStr;
}
然后,在您的代码中(我用您的示例RSS尝试了这一点,它似乎在IE8中做到了这一点)
希望有帮助 。是否需要转义
&
,或引用为&代码>?@jnpcl:jQuery正确地处理实体,如我的提琴中所示。也许我们需要查看您的一些XML。@BoltClock您的JSFIDLE在IE中不起作用。我打开了,IE中没有显示结果。
function Convert(xmlStr) {
if (window.ActiveXObject && (new window.ActiveXObject("Microsoft.XMLDOM") != null)) {
var xd = new window.ActiveXObject("Microsoft.XMLDOM");
if (xd) {
xd.loadXML(xmlStr);
return xd;
}
}
return xmlStr;
}
jQuery.get('news.xml', function(RSSqa) { // NEWS XML
RSSqa = Convert(RSSqa);
jQuery(RSSqa).find("item:contains('Q & A with'):lt(1)").each(function() {
/* rest of your code... */
});
});