HTML结构
<div id="test">
<span style="color:red">test1</span>test2
</div>
innerHTML:对象的起始位置到终止为止的全部内容,包括html标签
window.onload = function(){
var test = document.getElementById('test');
console.log(test.innerHTML);
}
输出为:test1test2
innerText:从起始位置到终止位置的内容,但它出去html标签
window.onload = function(){
var test = document.getElementById('test');
console.log(test.innerText);
}
输出为:test1test2
outerHTML:除包含innerHTML的全部内容外,还包含对象标本本身
window.onload = function(){
var test = document.getElementById('test');
console.log(test.innerText);
}
输出为:
test1test2
textContent:设置或返回指定节点的文本内容,以及它的所有后代
window.onload = function(){
var test = document.getElementById('test');
console.log(test.innerText);
}
输出为:test1test2