设置HTML的内容< span>与Javascript [英] Set content of HTML <span> with Javascript

查看:86
本文介绍了设置HTML的内容< span>与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的内容&lt; span&gt;与Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆