创建新文本节点并将其附加到 DOM 的最佳方法是什么?
我目前正在使用这个:
$('#someDiv').append(document.createTextNode('Some text'));
但这是最好的方法吗?还有其他选择吗?
更新
检查 jQuery 代码后,我发现我的变体可能是最好的......
这是 .text()
函数在 jQuery 中的实现方式:
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 );
},
so,.text()
函数调用.createTextNode()
方法,因此如果您希望节省调用额外操作的时间,最好使用直接文本的节点.append()
最佳答案
就像使用 .text() 一样简单
$("#output").text("Hello, World!");
关于javascript - 如何使用 jQuery 创建文本节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23016465/