Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

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

This is a follow-up of 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 class and style attributes & in between non-div text):

<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 correctProperty should 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"?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).

So I think you should use .value


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...