jQuery 中 document.createTextNode(' ') 的等价物是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3412376/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the equivalent of document.createTextNode(' ') in jQuery
提问by penguru
What can I use in jquery instead of document.createTextNode(' ')
js line.
我可以在 jquery 中使用什么而不是document.createTextNode(' ')
js 行。
I'm trying to do sth like this :
我正在尝试这样做:
js : divButtons.appendChild(document.createTextNode(' '));
js: divButtons.appendChild(document.createTextNode(' '));
jquery : $("#divButtons").append(?????);
查询: $("#divButtons").append(?????);
回答by usr
Please be careful with the existing answers. They show how to append HTML. HTML is not Text. Treating text as HTML is a) a bug and b) dangerous.
请注意现有的答案。它们展示了如何附加 HTML。HTML 不是文本。将文本视为 HTML 是 a) 错误和 b) 危险。
Here is a sane solution:
这是一个理智的解决方案:
$('selector').append(document.createTextNode('text < and > not & html!'))
This is using jQuery only for the append part. Nothing wrong with using createTextNode. jQuery uses createTextNode internally in its text(str)
function.
这仅将 jQuery 用于追加部分。使用 createTextNode 没有错。jQuery 在其text(str)
函数内部使用 createTextNode 。
回答by Christian
var jqTextNode = $(document.createTextNode(' '));
creates the jquery representation of a text node. i don't think there is a shortcut which works reliable.
创建文本节点的 jquery 表示。我认为没有可靠的捷径。
回答by Tim Wintle
I know this is an old question, but I was looking for the same thing...
我知道这是一个老问题,但我一直在寻找同样的东西......
The closest method that I'm aware of is the .text() method.
我所知道的最接近的方法是 .text() 方法。
This escapes text securely in the same way as creating a new TextNode, but it can only be used to specify the full contents of a node.
这以与创建新 TextNode 相同的方式安全地转义文本,但它只能用于指定节点的完整内容。
e.g.
例如
var mynode = jQuery('#node-id');
mynode.text('A<B implies B>A');
回答by Anurag
There is no equivalent in jQuery for createTextNode
. You can always use the DOM method, or write a jQuery wrapper around it. The closest thing you may be able to find is when creating new elements, you can specify the text part separately.
jQuery 中没有等效的createTextNode
. 您始终可以使用 DOM 方法,或者围绕它编写一个 jQuery 包装器。您可能能够找到的最接近的事情是在创建新元素时,您可以单独指定文本部分。
$('<div>', {
text: '<hello world>'
});