Javascript 获取div'内的span值;s
我试图遍历作为div子元素的所有span元素:Javascript 获取div'内的span值;s,javascript,jquery,Javascript,Jquery,我试图遍历作为div子元素的所有span元素: $('#recommendTextArea').children('span').each(function () { console.log(this.html()); }); 但是,我总是会遇到以下错误: Uncaught TypeError: Object #<HTMLSpanElement> has no method 'html' uncaughttypeerror:对象#没有
$('#recommendTextArea').children('span').each(function () {
console.log(this.html());
});
但是,我总是会遇到以下错误:
Uncaught TypeError: Object #<HTMLSpanElement> has no method 'html'
uncaughttypeerror:对象#没有方法“html”
我尝试将其更改为text(),但也不起作用请尝试以下操作:
$('#recommendTextArea').children('span').each(function () {
console.log($(this).text());
});
console.log($(this).html());
您试图对DOM元素而不是jQuery对象调用.html()
。1)尝试$(this.html()或$(this.text())
2) 您也可以使用$(“#recommendTextArea”).find(“span”)获取span是一个jQuery函数,必须在jQuery对象上调用。通过将jQuery对象传递给,可以从this
创建jQuery对象,如$(this)
在您的代码中,此是一个
,它没有任何名为.html()
的本机方法,因此JavaScript错误。也值得一读: