问题
I'm trying to change the CSS of an HTML element dynamically...for that purpose i am using innerHTML.
The problem I'm facing is, maybe Mozilla remembers the innerHTML of a textarea on the first posting. So if the user edits the text in textarea, it fails to show new text and shows original text in textarea. In IE it works fine.
What's wrong with innerHTML and mozilla firefox ?
<html>
<head>
<title></title>
<script type="text/javascript">
function abc()
{
alert(document.getElementById("c").innerHTML);
}
</script>
</head>
<body>
<textarea id="c" onclick="abc()">hello...</textarea>
</body>
</html>
回答1:
Regarding your requirement, why don't you just do:
document.getElementById("c").className = "myCSSClass";
because using innerHTML
to change the CSS class of an element seems pretty weird...
回答2:
use .value for TextArea
回答3:
cannot contain child elements, but value.
Elements/value inside are treated as plain text not html.
You should use value property to retrieve its contents.
use:
document.getElementById('c').value
回答4:
Why do you want to use innerHTML?
The use of innerHTML
is to dynamically insert the content to a specific tag using id
.
you can use alert(document.getElementById("c").value);
回答5:
Always use the value
property to change the value of a <textarea>
.
It seems a common misconception that innerHTML
is a viable or even superior way of doing this. It is neither.
What happens is that changing the innerHTML
property of a text area will work only until the value has been changed, either by the user editing the contents of the textarea or by script changing the value
property, because the child nodes of a textarea element represent only its initial value.
It's nothing to do with the standards-compliance or otherwise of innerHTML
, or that only a single text node is legal within a <textarea>
element: you get exactly the same behaviour if you try to change the nodeValue
property of a text node contained within the <textarea>
element.
来源:https://stackoverflow.com/questions/4274686/why-mozilla-ff-does-not-update-innerhtml