回顾下几组DOM插入有关的方法:
innerHTML 设置或获取位于对象起始和结束标签内的 HTML
outerHTML 设置或获取对象及其内容的 HTML 形式
innerText 和 outerText 在读取的时候是一样的,只是在设置的时候 outerText 会连带标签一起替换成目标文本
firefox不支持innerText,但是可以用textContent作为替代方案。
jQuery封装的方法html,text,val(放到属性一章)
.html()用为读取和修改元素的HTML标签
.text()用来读取或修改元素的纯文本内容
.val()用来读取或修改表单元素的value
html
获取集合中第一个匹配元素的 HTML 内容。
取值
在一个 HTML 文档中, 我们可以使用 .html() 方法来获取任意一个元素的内容。 如果选择器匹配多个元素,那么只有第一个匹配元素的 HTML 内容会被获取。
源码部分可见jQuery.access在属性节点操作的时候就详解过了,就是合并分解多个参数,细分到每一个流程调用中,通过回调接收分解后的参数。
可见针对nodeType === 1的节点是通过浏览器接口innerHTML返回需要取的值。
有些浏览器返回的结果可能不是原始文档的 HTML 源代码。例如,如果属性值只包含字母数字字符,Internet Explorer有时丢弃包裹属性值的引号。
html: function( value ) {
return jQuery.access( this, function( value ) {
var elem = this[ 0 ] || {},
i = 0, l = this.length;
if ( value === undefined && elem.nodeType === 1 ) {
return elem.innerHTML;
}
}, null, value, arguments.length )
设值
.html() 方法对 XML 文档无效。
我们可以使用 .html() 来设置元素的内容,这些元素中的任何内容会完全被新的内容取代。
此外,用新的内容替换这些元素前,jQuery从子元素删除其他结构,如数据和事件处理程序,防止内存溢出,对插入的值做一下过滤处理。
必须是字符串,而且不能暴行script|style|link,并且不是tr,表格等元素。
最后通过innerHTML覆盖节点,防止内存溢出需要jQuery.cleanData清理节点上的事件与数据。
总结: elem.innerHTML 也就是从对象的起始位置到终止位置的全部内容,包括Html标签。
.text()
得到匹配元素集合中每个元素的文本内容结合,包括他们的后代,或设置匹配元素集合中每个元素的文本内容为指定的文本内容。
.text() 在 XML 和 HTML文档中都能使用。
.text() 方法返回一个字符串,包含所有匹配元素的合并文本。(由于在不同的浏览器中的HTML解析器的变化,返回的文本中换行和其他空白可能会有所不同。
text: function( value ) {
return jQuery.access( this, function( value ) {
return value === undefined ?
jQuery.text( this ) :
this.empty().append( ( this[ 0 ] && this[ 0 ].ownerDocument || document ).createTextNode( value ) );
}, null, value, arguments.length );
}
取值
jQuery.text( this ) 实际调用Sizzle.getText。
但是实际上jQuery没有用innerText获取文本的值,http://bugs.jquery.com/ticket/11153,大概就是在IE8中新节点插入会保留所有回车。
所以jQuery采用了textContent获取文本值,textContent本身是dom3规范的,可以兼容火狐下的innerText问题。
设值
考虑下,如果文本的值不仅仅是字符串,可能是带有标签的:
<p>This is a test.</p>
这种情况下,当然就不能直接套用
elem.textContent = <p>This is a test.</p>。
我们必须意识到这种方法提供了必要的字符串从提供的正确的HTML中脱离出来。
jQuery这样做, 他调用DOM 方法.createTextNode(), 一种替代的特殊字符与HTML对应(比如< 替换为 < )方法。
总结
1 .text() 在XML 和 HTML 文档中都能使用。
2 .text() 方法返回一个字符串,包含所有匹配元素的合并文本。(由于在不同的浏览器中的HTML解析器的变化,返回的文本中换行和其他空白可能会有所不同)。
3 .text() 方法不能使用在 input 元素或 scripts 元素上。 input 或 textarea 需要使用 .val() 方法获取或设置文本值。得到scripts元素的值,使用.html()方法。
<!doctype html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<title>DOM插入</title>
</head>
<body>
<button id="test1">html取值操作</button>
<button id="test2">html设值操作</button>
<button id="test3">text取值操作</button>
<button id="test4">text设值操作</button>
<div class="container">
<div class="inner">Hello</div>
<div class="inner">Goodbye</div>
</div>
<p>
<b>TestP1</b>Paragraph.
</p>
<p>TestP2</p>
<script type="text/javascript">
function html(value) {
var elem = this[0] || {}, //第一个匹配元素
i = 0,
l = this.length;
if (value === undefined //取值操作,只取第一个匹配的元素
&& elem.nodeType === 1) {
return elem.innerHTML;
}
for (; i < l; i++) { // 设值操作 遍历匹配的所有元素
elem = this[i] || {};
if (elem.nodeType === 1) {
elem.innerHTML = value;// 作用:给所有元素节点设置值
}
}
}
function getText(elem) {
var node,
ret = "",
i = 0,
nodeType = elem.nodeType;
if (!nodeType) {
//如果没有节点类型,表示是一个数组
while ((node = elem[i++])) {
// 不遍历注释节点
ret += getText(node);
}
} else if (nodeType === 1 || nodeType === 9 || nodeType === 11) {//元素、文档和文档碎片节点
if (typeof elem.textContent === "string") {
return elem.textContent;
} else { //该元素下有子元素
for (elem = elem.firstChild; elem; elem = elem.nextSibling) {
ret += getText(elem);
}
}
} else if (nodeType === 3 || nodeType === 4) {// 文本和CDATA节点??
return elem.nodeValue;
}
return ret;
}
function empty() {
var elem,
i = 0;
for (;
(elem = this[i]) != null; i++) {//匹配的多个节点置为空
if (elem.nodeType === 1) {
elem.textContent = "";
}
}
return this;
}
function setText(value) {
empty.call(this)
if (this.nodeType === 1 || this.nodeType === 11 || this.nodeType === 9) {//元素,文档,文档碎片节点
this.textContent = value;
}
}
function text(value) {
return value === undefined ?
//取值
getText(this) :
//清理
setText.call(this, value)
}
$('#test1').click(function() {
var inner = document.querySelectorAll('.inner')
alert(html.call(inner))
})
$('#test2').click(function() {
var inner = document.querySelectorAll('.inner')
html.call(inner,'慕课网')
})
$('#test3').click(function() {
var p = document.querySelectorAll("p")[0]
alert(text.call(p))
})
$('#test4').click(function() {
var p = document.querySelectorAll("p")[0]
text.call(p,'慕课网')
})
</script>
</body>
</html>