jQuery html()问题 [英] Jquery html() issue
本文介绍了jQuery html()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码将html
标记放入我的array
中
I am trying to put the html
markup into my array
with the following codes
//my htmlData which is entered by user so it could be varies.
<em>test</em> here for the testing purpose
second line <strong>texts</strong> here
我想通过
var data = [];
$(htmlData).contents().each(function(){
data.push($(this).html().trim());
}
但是我说Uncaught TypeError: Cannot call method 'trim' of null.
不确定为什么$(this).html()
将返回null
.
有人可以在这里帮助我吗?非常感谢!
Can anyone help me here? Thanks a lot!
推荐答案
text nodes.
可能是问题,因为它们没有innerHTML
属性
尝试过滤掉它们
Should be the issue with the text nodes.
As they do not have the innerHTML
property
Try filtering them out
var data = [];
$(htmlData).contents().each(function(){
var nodeType = this.nodeType;
if(nodeType === 1) { // Will only select element nodes
data.push($.trim($(this).html()));
}
else if(nodeType === 3) {
data.push($.trim(this.nodeValue));
}
}
您可以使用.nodeValue
访问etext节点的数据
You can use .nodeValue
to access the data of th etext node
这篇关于jQuery html()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文