Copy the codeThe code is as follows:
<script type="text/javascript">
("testDiv").innerHTML="Dynamicly created div";
</script>
And I should still enjoy using it, but how many people know that this is the wrong way! The reason for the error:
(1) Change the structure of the page when the page is loaded. In IE6, if the network becomes slower or the page content is too large, the "termination operation" error will occur. That is to say, "Never change the Dom model of the page when the page is loaded."
(2) Adding elements by modifying HTML content does not comply with the Dom standard. In actual work, I have also encountered using this method to modify content. Some browsers cannot immediately display the added elements, because the display engines of different browsers are different. However, if we use Dom's CreateElement to create objects, it is almost possible in all browsers. However, if the passed in jQuery is a complete HTML string, innerHTML is also used internally. Therefore, it is not completely negating the use of innerHTML functions. So please abandon this old knowledge from now on and use the correct method introduced below to program.
This article will not introduce the creation of elements using HTML DOM in detail. Here is a simple example: The first correct way:
Copy the codeThe code is as follows:
//Create elements using Dom standard
var select = ("select");
[0] = new Option("Add-in 1", "value1");
[1] = new Option("Add-in 2", "value2");
= "2";
var object = (select);
By using the method we can create Dom elements and then add them to the specified object through the appendChild method.
The second way: Use Jquery
When an HTML string is an element without attributes, the element is created internally, for example:
// jQuery internally creates elements:
$("").css("border","solid 1px #FF0000").html("Dynamic created div").appendTo(testDiv);
Otherwise, use the innerHTML method to create elements:
// jQuery uses innerHTML to create elements:
$("Dynamic created div").appendTo(testDiv)