SoFunction
Updated on 2025-02-28

Solution to the unknown runtime error when setting innerHTML in Javascript under IE

Copy the codeThe code is as follows:

<script>
("trone").innerHTML = "<td>haha</td>";
</script>
<tr >
</tr>

In IE, sometimes "unknown runtime error" will occur, but not in firefox.

This is mainly because when IE writes innerHTML, it will check whether the element has the requirements to be the html object container in these contents. For example, if you add html code containing <li> to a p, this will make an error. So if you find these errors appear, try to do two things:
1. Check whether the html code you are trying to add to innerHTML contains broken html tags, such as <li></li> without <ul></ul> surrounding <li></li>.
2. Change your container to those more "inclusive" tags, such as <span></span>, <div></div>
It is particularly important to note that adding the correct format of <td> tags into <tbody>, <table>, and <tr> tags may cause errors, so you should avoid using these tags as containers. If necessary, you can use <td> as containers and then nest a layer of tables.

So, if it is
<td ></td>Use ("trone").innerHTML="new";
Solution You can use javascript to create <tr><td> and other tags dynamically, instead of directly modifying them with innerHTML.