在JQuery中使用Load()获取XML属性 [英] Getting XML attributes with Load() in JQuery
问题描述
我有此代码,该代码有效:
I have this code, which works:
$('.invest-port-thumb a').mouseenter(function() {
$('#slider-name').load(this.href + ' cName');
});
加载此XML:
<fragment>
<cName cind="Industrial" stat="Active">ABC Company</cName>
<hq>Chicago, IL</hq>
</fragment>
如何修改此代码以加载cName
的cind
属性?
How should I modify this code to load the cind
attribute of cName
?
推荐答案
jQuery在加载时不会自动为您解析XML.但是,您可以使用jQuery来解析XML.请查看此页面的示例: http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html .基本上,您加载XML并执行如下选择和修改:
jQuery will not automatically parse the XML for you on load. But, you can use jQuery to parse the XML. Check out this page for examples: http://www.xml.com/pub/a/2007/10/10/jquery-and-xml.html. Basically, you load the XML and perform your selections and modifications like this:
这是一个入门的演示:如果XML中只有一个片段和一个cName,这样做是安全的:
Here's a demo to get you started: if you only have one fragment and one cName in your XML, it's safe to do this:
var cind = $(myXML).find("fragment cName").attr("cind");
似乎您正在使用load()加载XML片段.相反,请尝试将整个XML内容加载到一个变量中,并使用我上面提供的代码行从中解析出cind属性.
It appears that you are using load() to load a fragment of the XML. Instead, try loading the entire XML content to a variable, and parse the cind attribute out of it using the line of code I gave you above.
尝试一下:
$('.invest-port-thumb a').mouseenter(function() {
$.get(this.href, function(response){
var cind = $(response).find("fragment cName").attr("cind");
$('#slider-name').html(cind);
})
});
这篇关于在JQuery中使用Load()获取XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!