Javascript document.getElementById().value 在 chrome 中返回未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10694661/
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
document.getElementById().value return undefined in chrome
提问by run
<div id="hour" style="display: none">2</div>
JavaScript code:
JavaScript 代码:
<script type="text/javascript">
var _h = document.getElementById('hour').value
alert(_h);
</script>
Chrome returns undefined
. What is the problem?
铬返回undefined
。问题是什么?
回答by nnnnnn
The .value
property applies to form elements (inputs), not divs. The simplest way to get the contents of your div element is with .innerHTML
:
该.value
属性适用于表单元素(输入),而不是 div。获取 div 元素内容的最简单方法是.innerHTML
:
document.getElementById('hour').innerHTML;
回答by epascarello
divs do not have a value. It is not an input.
div 没有价值。它不是输入。
You want to use innerHTML or innerText/textContent.
您想使用innerHTML 或innerText/textContent。
回答by neohope
document.getElementById("hour").innerText
or
或者
document.getElementById("hour").innerHTML