我有困难循环通过一个xmlDoc,任何帮助将不胜感激。这是我的原始html
<ul class="overview">
<li class="item first odd">Li content 1</li>
<li class="item even">li content 2</li>
......
//I have a total of 16 LIs in here
<li class="item last even">li content last 16</li>
</ul>下面是我的javascript:
<script>
var myHtmlContent = jQuery( "ul.overview" ).html();
var xmlDoc = new DOMParser().parseFromString(myHtmlContent , "text/html");
var root = xmlDoc.getElementsByTagName ("li");
var len = root.length;
alert(len); // I get the right number
if( len > 8 ) {
for(var i=0; i<len; i++){
var myCurrentLiContent = root[i].childNodes[0].nodeValue;
alert(myCurrentLiContent );
/*
My goal is to get the whole text like this "<li class="item
first odd">Li content 1</li>" so I can mannualy render later on,
but I get nothing from myCurrentLiContent variable...
So please help.
*/
}
}
</script>发布于 2018-01-03 02:40:37
你得到的len等于3,但是你要检查len是否大于8,因为它不是,所以你的代码不会执行。删除if语句,代码就可以正常工作了。
发布于 2018-01-03 04:05:06
<ul class="overview">
...
var myHtmlContent = jQuery( "ul.overview" ).html();在本例中,myHtmlContent是一个列表,而不是一个节点。创建这个id="overview"和jQuery('ul#overview') (可能)来修复这个问题。最好使用带有变量监视器的调试器来诊断这些问题,或者至少使用大量的console.log()语句,而不是稍后尝试从一个完整的blob中对其进行剖析。
发布于 2018-01-03 05:13:15
啊!!我找到了一个适合我的解决方案。而不是这个
<script>
....
var myCurrentLiContent = root[i].childNodes[0].nodeValue,
....
</script> 我要做的就是这样
<script>
....
var myCurrentLiContent = root[i].innerHtml
....
</script>谢谢你所有的帮助,这些对我来说都很有价值。谢谢!
https://stackoverflow.com/questions/48066013
复制相似问题