Javascript 如何在 document.createElement("span"); 之后将文本添加到 span 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26542652/
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
How to add text into span after document.createElement("span");?
提问by Joe
I'm trying to write text into an element span with function .text(), but I got this error UncaughtTypeError: undefined is not a function, and I also tried function append(), innerHtml(), createTextNode() with no success.
我正在尝试使用函数 .text() 将文本写入元素范围,但我收到此错误 UncaughtTypeError: undefined is not a function,我也尝试了函数 append()、innerHtml()、createTextNode() 但没有成功.
What am I doing wrong?
我究竟做错了什么?
var closeSpan = document.createElement("span");
closeSpan.setAttribute("class","sr-only");
closeSpan.text("Close"); //UncaughtTypeError: undefined is not a function
OR
或者
var closeSpan = document.createElement("span");
closeSpan.setAttribute("class","sr-only");
closeSpan.append("Close"); //UncaughtTypeError: undefined is not a function
回答by Louis
Since you are starting with pure DOM code, I'd suggest continuing with pure DOM code:
由于您是从纯 DOM 代码开始的,我建议您继续使用纯 DOM 代码:
var closeSpan = document.createElement("span");
closeSpan.setAttribute("class","sr-only");
closeSpan.textContent = "Close";
In other words, just set textContentto the value you want.
换句话说,只需设置textContent为您想要的值。
If compatibility with IE 8 or earlier matters to you, note that textContentdoes not exist for those older browsers. For those older ones you'd have to use innerText(which works on IE 8 but is not part of any standard) or innerHTML. See the MDN page on textContent(which I link to above) for a discussion of the differences between these fields.
如果与 IE 8 或更早版本的兼容性对您很重要,请注意textContent那些较旧的浏览器不存在。对于那些较旧的,您必须使用innerText(适用于 IE 8,但不属于任何标准)或innerHTML. 有关textContent这些字段之间差异的讨论,请参阅 MDN 页面(我在上面链接到该页面)。
回答by Sean
If you don'twant to use jquery, you would use closeSpan.appendChildand document.createTextNodelike so:
如果你不希望使用jQuery的,你可以使用closeSpan.appendChild和document.createTextNode像这样:
var closeSpan = document.createElement("span");
closeSpan.setAttribute("class","sr-only");
closeSpan.appendChild(document.createTextNode("Close"));
This method maximizes cross browser compatibility. It will work in all browsers, including old versions of IE.
这种方法最大限度地提高了跨浏览器的兼容性。它适用于所有浏览器,包括旧版本的 IE。
If you dowant to use jquery, you could do this in one line:
如果您确实想使用 jquery,可以在一行中执行此操作:
var closeSpan = $("<span></span>").addClass("sr-only").text("Close")[0];
回答by anusha
You can try this also
你也可以试试这个
var closeSpan = document.createElement("span");
closeSpan.setAttribute("class","sr-only");
closeSpan.html("Close");
回答by Joe
Assuming you want to add the "span" tag to a div with id "myDiv":
假设您想将“span”标签添加到 id 为“myDiv”的 div:
var span = $("span"); // create element span
$(span).html("your text"); // add text
$("#myDiv").append($(span)); // append it to the div element

