Home > Article > Web Front-end > Notes on using Textarea_HTML/Xhtml_Web page production
Why mention textarea in particular? Because actually the textarea node is very special. Because this node is special, it is interpreted differently under IE and other browsers.
Give it a try, haha! In fact, the main reason is to help a senior brother fix a BUG today. Specifically, it is because the data pulled from the server and inserted into the textarea failed. This situation only occurs in IE. Everything works fine in other browsers!
First of all, why is textarea special? In all form plug-ins, the value of textarea is written between two opening and closing tags, so its value can be regarded as a text node from the perspective of DOM, which is unique to textarea. Because of this feature, you can still change the text of the textarea when you modify its innerHTML value. Although IE supports this, it does not allow one thing: dynamically inserting some html tags.
You might as well do an experiment:
Insert a piece of HTML code through JS under IE, using the innerHTML attribute instead of value or innerText.
I have not tested IE9, but IE6, 7, and 8 will all report errors.
It is estimated that for security reasons, JS is not allowed to dynamically insert html into the textarea, but careful people can find that in fact, if you type the html code manually, the textarea is acceptable. I think this actually goes through a process: character escaping. Yes, this is the only reason that can explain the success of manually entering html code.
So, in my opinion, since the textareas of so many browsers do not support the display of html, why do we need to use the innerHTML attribute when operating JS? That is to say, whether you use value to insert html code or innerHTML to insert html code, it will not be parsed and displayed (that's why there is a rich text editor instead of textarea), so why not use value to set the value of textarea?
So I think that setting the value of textarea should not be through innerHTML (of course it is not possible to be innerText, because of firefox), but should be set through value. This is something developers should pay attention to. Because some of the code for children's shoes that I have seen recently always like to use innerHTML. In fact, I think it all depends on the occasion.