JavaScript 通过 .value 或 .innerHTML 获取 TextArea 输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5314186/
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
JavaScript get TextArea input via .value or .innerHTML?
提问by Francisc
Is it ok to get the value of a textarea element in JavaScript with myTextArea.value
or should I use myTextArea.innerHTML
?
在 JavaScript 中使用myTextArea.value
或应该使用获取 textarea 元素的值是否可以myTextArea.innerHTML
?
Thank you.
谢谢你。
回答by jessegavin
You should use .value
你应该使用 .value
myTextArea.value
回答by Amir Md Amiruzzaman
For div and span, you can use innerHTML, but for textarea use value. Please see the example below.
对于 div 和 span,您可以使用 innerHTML,但对于 textarea 使用 value。请看下面的例子。
<script language="javascript/text">
document.getElementById("spanText").innerHTML ="text";
document.getElementById("divText").innerHTML ="text";
document.getElementById("textArea").value ="text";
</script>
<span id="spanText"></span>
<div id="divText"></div>
<textarea id="textArea"></textArea>
回答by SuperOP535
One difference is that you can use HTML entities with .innerHTML
一个区别是您可以将 HTML 实体与 .innerHTML 一起使用
document.getElementById('t1').innerHTML = '<>&';
document.getElementById('t2').value = '<>&';
<textarea id="t1"></textarea>
<textarea id="t2"></textarea>
回答by Jimmy Marvosh
Don't use innerHTML use value e.g. document.getElementById(name).value
不要使用 innerHTML 使用值,例如 document.getElementById(name).value
回答by ryanneufeld
The answer depends on your situation.
答案取决于您的情况。
I would personally use .value as that's what the other form inputs provide. It's easier to be in the habit of doing it that way.
我个人会使用 .value ,因为这是其他表单输入提供的。养成这样做的习惯会更容易。