设置HTML的内容< span>与Javascript [英] Set content of HTML <span> with Javascript
问题描述
在一个网页中,我打电话给一个WebService,它给了我一个整数值。我需要在一段文字中显示这个值。我目前使用HTML < span>
。
到目前为止,我发现了两种放置方法我的价值在一个跨度。 innerText()
是一种IE专有的方式, innerHTML()
是一种不符合标准的方式,广泛支持。
在< span>
和<$之间设置正确的符合标准的文本方式c#c>< / span> 来自Javascript?
浏览器安全。
示例: http:// jsfiddle .net / kv9pw /
var span = document.getElementById('someID');
while(span.firstChild){
span.removeChild(span.firstChild);
}
span.appendChild(document.createTextNode(some new content));
In a webpage I am calling a WebService that gives me an integer value. I need to display this value in a block of text. I am currently using an HTML <span>
.
So far, I've found two methods of putting my value in a span. innerText()
is an IE proprietary way of doing it and innerHTML()
is a non-standards compliant way, although widely supported.
What is the correct standards compliant way of setting the text between <span>
and </span>
from Javascript?
This is standards compliant and cross-browser safe.
Example: http://jsfiddle.net/kv9pw/
var span = document.getElementById('someID');
while( span.firstChild ) {
span.removeChild( span.firstChild );
}
span.appendChild( document.createTextNode("some new content") );
这篇关于设置HTML的内容< span>与Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!