Notes on using textarea


Why mention textarea in particular? Because in fact, the textarea node is very special. Because this node is special, it is interpreted differently in IE and other browsers.

Sell it, ha ha! In fact, the main reason is that I helped a senior brother fix a bug today. Specifically, I failed to pull data from the server and insert it into textarea. This situation only occurs in ie. All other browsers are OK!

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 in the DOM perspective, which is unique to textarea. Because of this feature, you can still change the text of textarea when you modify its innerHTML value, while ie supports this, but it does not allow one thing: dynamic insertion of some HTML tags.

You might as well do an experiment:

Insert a piece of HTML code through JS under ie, using innerHTML attribute instead of value or innerText.

I didn’t test IE9 +, but IE6, 7 and 8 all reported errors.

It’s probably for security reasons that JS is not allowed to dynamically insert HTML into textarea, but careful people can find that textarea is acceptable if you manually type HTML code. I think it has gone through a process: character escape. Yes, that’s the only explanation for the success of entering HTML code manually.

So, in my opinion, since so many browsers don’t support HTML display in textarea, why use innerHTML attribute when JS is operating? That is to say, no matter you use value to insert HTML code or innerHTML to insert HTML code, it will not be displayed after parsing (so there will be rich text editor instead of textarea). Why not use value to set textarea value?

So I think that the value of textarea should not be set through innerHTML (of course, innerText, because of Firefox), but through value. This is something developers should pay attention to. Because the code of some children’s shoes I read recently always likes to use innerHTML. In fact, I think it’s all about separation and combination.

Recommended Today

The way of nonlinear optimization

Mathematical knowledge 1、 Nonlinear functionLinear function is another name of a function of first degree, then nonlinear function means that the function image is not a function of a straight line.Nonlinear functions include exponential function, power function, logarithmic function, polynomial function and so on. 2、 Taylor expansion1. Taylor formula:Taylor’s formula is to add a_ The […]