javascript 无法将值动态设置为 Label

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6122461/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 19:34:21  来源:igfitidea点击:

Unable to set the value into Label dynamically

javascriptprototypejs

提问by Samuli Hakoniemi

I have a label as shown

我有一个如图所示的标签

<label id="label1"></label>

I am trying to set the value in it using

我正在尝试使用

document.getElementById('label1').text = "ddd";

And i also i tried using Prototype as

我也尝试使用 Prototype 作为

$('label1').text = date1;

But still its not working

但仍然无法正常工作

回答by Samuli Hakoniemi

document.getElementById("label1").innerText = "foobar"

or alternatively

或者

document.getElementById("label1").innerHTML = "<b>fatfoo</b>";

回答by dogbane

Use the innerText or innerHTML property:

使用 innerText 或 innerHTML 属性:

document.getElementById("label1").innerText="ddd";

回答by mplungjan

Try .innerHTML, innerText, .textContent, .nodeValue

尝试 .innerHTML、innerText、.textContent、.nodeValue

I have only seen .text in selects

我只在选择中看到 .text

(I need to stop commenting and answer instead...)

(我需要停止评论并回答......)

回答by RobG

A label element is supposed to enclose a form control, e.g.

标签元素应该包含一个表单控件,例如

<label id="label1" for="firstName">First name:
  <input type="text" name="firstName" id="firstName"></label>

If that is how you are using it, then you don't want to replace it's innerHTML, innerTextor textContentas doing so will completely replace the content of the label (removing any elements within it). If you aren't using the label this way, you should be using some other element, probably a span.

如果这是您使用它的方式,那么您不想替换它的innerHTMLinnerTexttextContent因为这样做将完全替换标签的内容(删除其中的任何元素)。如果您不以这种方式使用标签,则应该使用其他一些元素,可能是跨度。

While innerHTMLis widely supported, it's only been standardised in HTML5, which is still a draft. However, you can usually assume support and setting it is pretty consistent across browsers. Getting the property has foibles and isn't very consistent if it's anything other than trivial markup (e.g. text and simple inline elements).

虽然innerHTML得到了广泛的支持,但它只在HTML5 中被标准化,这仍然是一个草案。但是,您通常可以假设对浏览器的支持和设置非常一致。获取属性有弱点,如果不是简单的标记(例如文本和简单的内联元素),则它不是很一致。

The innerTextproperty is a Microsoft proprietary property that is supported by IE and some other browsers, it definitely should notbe used without feature testing. Similarly, the W3C DOM 3 textContentproperty is not widely supported, but is generally supported by those browsers that don't support innerText(some support both).

的innerText属性是通过IE浏览器和其他一些浏览器所支持的微软私有财产,它绝对不应该被无功能测试中使用。类似地,W3C DOM 3 textContent属性没有得到广泛支持,但通常被那些不支持innerText 的浏览器支持(有些同时支持)。

It's pretty easy to do a feature test and select which one to use, such as:

进行功能测试并选择要使用的功能非常容易,例如:

if (typeof element.textContent == 'string') {
  // use textContent property

} else if (typeof element.innerText == 'string') {
  // use innerText property
}

where elementis a reference to a DOM element node.

其中element是对 DOM 元素节点的引用。

It's probably best to wrap the text you want to replace in a span element and replace the content of that, either using innerHTMLor, if it contains a single text node, its firstChild.data.

最好将要替换的文本包装在 span 元素中并替换其中的内容,使用innerHTML或者如果它包含单个文本节点,则使用其firstChild.data.

回答by Ankur Kumawat

Use the following

使用以下

$('#label1').text(date1);

$('#label1').text(date1);

text is a method here.

text 是这里的一种方法。