Javascript Span.innerHTML 返回文本但 Span.value 返回未定义?

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

Span.innerHTML returns text but Span.value returns undefined?

javascriptdom

提问by Ryan

I have a function that populates a pages with something like this

我有一个函数可以用这样的东西填充页面

<span id="span_title_'+result_no+'">'+title+'</span>

and then I have another function that has this:

然后我有另一个功能:

      document.getElementById("span_title_"+which_table).innerHTML="asg";
alert(document.getElementById("span_title_"+which_table).value);

The strange thing is the first (innerHTML) call works perfectly, the second one, the alert, gives me "undefined"

奇怪的是第一个(innerHTML)调用完美无缺,第二个,警报,给了我“ undefined

Any idea why this is?

知道这是为什么吗?

回答by Ates Goral

<span>DOM elements don't have a valueproperty. Use innerHTMLto read the contents.

<span>DOM 元素没有value属性。使用innerHTML阅读内容。

回答by Senad Me?kin

span doesn't have attribute with the name "value" only innnerHTML, you should use innerHTML for second call.

span 没有名称为“value”的属性,只有innerHTML,您应该使用innerHTML 进行第二次调用。