text vs innerHTML in JQuery

核能气质少年 提交于 2019-12-07 03:54:45

问题


I get the same result from both methods, but not sure why. Research on SO tells me this:

.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.

however, further research tells me this: The real issue is that text() and innerHTML operate on completely different objects.

Can I get some clarification?

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'

回答1:


.text() will return a string representation of all the text nodes in that element while .html() will give you the presentation of all nodes.



来源:https://stackoverflow.com/questions/16743737/text-vs-innerhtml-in-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!