文本vs内部HTML在jQuery中 [英] text vs innerHTML in JQuery
问题描述
我从两种方法中得到相同的结果,但不确定为什么.对SO的研究告诉我:
I get the same result from both methods, but not sure why. Research on SO tells me this:
.text()仅返回该元素及其所有后代元素的文本,其中.innerHTML返回该元素中的所有HTML.
.text() returns JUST the text of that element and all of its descendant elements, where as .innerHTML returns all of the HTML in that element.
但是,进一步的研究告诉我: 真正的问题是text()和innerHTML在完全不同的对象上运行.
however, further research tells me this: The real issue is that text() and innerHTML operate on completely different objects.
我可以澄清一下吗?
HTML
<table id="table2">
<th> Col1 </th>
<th> Col2 </th>
<tbody>
<tr>
<td id="data">456</td>
</tr>
</tbody>
</table>
jQuery
$('td').click(function() {
var x=$(this).text();
alert(x); //returns '456'
})
var abc = document.getElementById('data');
var xyz = abc.innerHTML;
alert(xyz); //also returns '456'
推荐答案
.text()
将返回该元素中所有文本节点的字符串表示形式,而.html()
将为您提供所有节点的表示形式.
.text()
will return a string representation of all the text nodes in that element while .html()
will give you the presentation of all nodes.
这篇关于文本vs内部HTML在jQuery中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!