javascript 我应该为 <textarea> 使用 .innerHTML 还是 .value?

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

Should I use .innerHTML or .value for <textarea>?

javascripthtmlforms

提问by raumaan kidwai

This is a follow-up of Does this not work because I can't use a script in a div?

这是Does this not work because I can't use a script in a div?

Where I left off, I had this code:
Button script:

在我离开的地方,我有这个代码:
按钮脚本:

<script>
    var correctProperty = "value";
    function run(){
        document.getElementsByTagName("head")["0"].innerHTML += eval(document.getElementById("editorHead")[correctProperty]);
        document.getElementById("result").innerHTML = eval(document.getElementById("editorBody")[correctProperty]);
    }
</script>

The textareas (without classand styleattributes & in between non-div text):

文本区域(没有classstyle属性 & 在非 div 文本之间):

<textarea id="editorHead" rows="20"></textarea>
<textarea id="editorBody" rows="20"></textarea>
<div id="result"></div>

Currently this doesn't work, and one of my answers from before (the accepted one) said that correctPropertyshould be set to "innerHTML". So should it? Or is the problem an interference with window(like before)? Or, ... should I put it in a form and then use "value"?

目前这不起作用,我之前的一个答案(已接受的答案)说correctProperty应该设置为"innerHTML". 那么应该吗?或者问题是干扰window(像以前一样)?或者,......我应该把它放在一个表格中然后使用"value"吗?

采纳答案by Pointy

You access and update the value of a textarea element through the "value" property.

您可以通过“value”属性访问和更新 textarea 元素的值。

Try:

尝试:

 var theValue = document.getElementById("editorHead").value;

Updating the <html>element doesn't make much sense.

更新<html>元素没有多大意义。

回答by Adeel Raza Azeemi

Javascript innerHTML does update the text in textarea but it doesn't show yours changes in the browser window; if you use it for the second time. for example, If you had some button that updates the text in textarea; this button would insert text for the first time but on second time although it would insert text in textarea (as can be proved by firebug or chrome developer tool) but this would not be shown in browser window. Instead if you use value this would be shown in browser (I had test it in chrome and firefox).

Javascript innerHTML 会更新 textarea 中的文本,但不会在浏览器窗口中显示您的更改;如果你第二次使用它。例如,如果您有一些按钮可以更新 textarea 中的文本;此按钮将第一次插入文本,但第二次将插入文本,尽管它会在 textarea 中插入文本(可以通过 firebug 或 chrome 开发人员工具证明),但这不会显示在浏览器窗口中。相反,如果您使用 value,这将显示在浏览器中(我已经在 chrome 和 firefox 中对其进行了测试)。

So I think you should use .value

所以我认为你应该使用 .value

回答by Grimnoff

No idea why you are using eval... below is corrected.. outside of that not sure what you are trying to accomplish. Both value and innerHTML will pull from textareas, the question is what you want only text in the return or possible HTML values.

不知道您为什么使用 eval... 下面已更正...除此之外不确定您要完成什么。value 和 innerHTML 都将从 textarea 中提取,问题是您只想要返回的文本或可能的 HTML 值。

function run(){
    document.getElementsByTagName("html")[0].innerHTML += document.getElementById("editorHead").innerHTML;
    document.getElementById("result").innerHTML = document.getElementById("editorBody").innerHTML;
}