Javascript 奇怪的jqueryxml问题
我在XML文档中有一个引号列表。每个报价的包装方式如下:Javascript 奇怪的jqueryxml问题,javascript,jquery,xml,Javascript,Jquery,Xml,我在XML文档中有一个引号列表。每个报价的包装方式如下: <Item> <Quote>This is a quote!</Quote> <Source>-- this is the Source of the Quote!</Source> </Item> 这是一个报价! --这是报价的来源! 以下是jQuery: var html = ''; var tmpl = '<li cl
<Item>
<Quote>This is a quote!</Quote>
<Source>-- this is the Source of the Quote!</Source>
</Item>
这是一个报价!
--这是报价的来源!
以下是jQuery:
var html = '';
var tmpl = '<li class=""><p class="quote">__quote</p><p class="source">__source</p></li>';
$(quoteObj).find('Item').each(function(){
$that = $(this);
var _quote = $that.children('Quote').text();
var _source = $that.children('Source').text();
var qhtml = tmpl.replace('__quote', _quote).replace('__source', _source);
html += qhtml;
});
return html;
var html='';
var tmpl='\uuuquote
\uuuuu source
;
$(quoteObj).find('Item').each(function(){
$that=$(this);
var _quote=$that.children('quote').text();
var_source=$that.children('source').text();
var qhtml=tmpl.replace(“”,“”)。replace(“”,“”,“”);
html+=qhtml;
});
返回html;
在最终产品中,引号
都在那里,但是来源
没有。我一辈子都不知道为什么。什么东西就在我面前,我看不见
回答评论的其他信息:
var tmpl
行来显示我在循环中替换的内容。由于第二个
是空的,而不是包含字符串,因此正在替换\u引号
,并且至少正在对\u源代码
执行操作this
,或.children()
方法操作的某种问题,但我仍然找不到它
最后一点注意:
将XML标记大小写更改为首字母大写,这在相关文档中。刚刚尝试过,我唯一需要更改的是
find
行以匹配XML节点的大小写,例如
$(quoteObj).find('ITEM').each( function() {
我也确实更改了$that
赋值行以包含var
关键字,但在我这么做之前它已经工作了
var $that = $(this);
jQuery不解析XML。将XML字符串传递给
$()
只需将该字符串指定为元素的innerHTML
属性,该属性具有可变和不可预测的结果。您需要使用浏览器的内置XML解析器自己解析XML,然后将生成的文档传递到jQuery:
var parseXml;
if (window.DOMParser) {
parseXml = function(xmlStr) {
return ( new window.DOMParser() ).parseFromString(xmlStr, "text/xml");
};
} else if (typeof window.ActiveXObject != "undefined" && new window.ActiveXObject("Microsoft.XMLDOM")) {
parseXml = function(xmlStr) {
var xmlDoc = new window.ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(xmlStr);
return xmlDoc;
};
} else {
parseXml = function() { return null; }
}
var xmlStr = "<Item><Quote>This is a quote!</Quote><Source>-- this is the Source of the Quote!</Source></Item>";
var xmlDoc = parseXml(xmlStr);
$xml = $(xmlDoc);
$xml.find('Item').each(function() {
// Do stuff with each item here
alert("Item");
});
var-parseXml;
if(window.DOMParser){
parseXml=函数(xmlStr){
return(new window.DOMParser()).parseFromString(xmlStr,“text/xml”);
};
}else if(typeof window.ActiveXObject!=“未定义”&&new window.ActiveXObject(“Microsoft.XMLDOM”)){
parseXml=函数(xmlStr){
var xmlDoc=new window.ActiveXObject(“Microsoft.XMLDOM”);
xmlDoc.async=“false”;
loadXML(xmlStr);
返回xmlDoc;
};
}否则{
parseXml=function(){return null;}
}
var xmlStr=“这是一个报价!——这是报价的来源!”;
var xmlDoc=parseXml(xmlStr);
$xml=$(xmlDoc);
$xml.find('Item')。每个(函数(){
//在这里做每件事
警报(“项目”);
});
您可以发布实际的XML吗?您的示例格式不正确(这可能是您的问题的答案,也可能只是您为快速向我们展示某些内容而采取的一种快捷方式),请在xml中输入源代码的值。它应该是sraight forward check,只需在firebut中点击断点,您就可以轻松地进行检查。@hollenback,只需提供更多信息,检查您是否通过.net面板从后端获得结果如果您获得结果,然后在循环中添加断点并检查值。什么是tmpl,什么是replace()?如果replace应该是本机字符串replace,那么第一个参数应该是RegExp,而不是字符串。@Dr.Molle,如果“本机”字符串replace指的是javascript,那么第一个参数可以是RegExp或字符串。这是您唯一做的更改吗?因为,对我来说,这些改变不起作用。你能在谷歌上用jQuery测试一下,看看结果如何吗?谷歌jQuery文件链接:这正是我使用的超级怪异。不知道为什么我可以让它与您的更改一起工作?是否要发布完整的示例?是的,从外部文件加载数据时它可以工作,但如果您将xml放入局部变量中,然后尝试使用jquery解析它,则它不工作。jquery不解析xml;将字符串传递给$()
函数只会将其指定为HTML元素的innerHTML
属性,这对于解析XML来说根本不可靠。您需要使用浏览器的内置XML解析器。看看我的答案。