1. Use the tbody element when appending rows to the table
to define the table to ensure that it can be used by all browsers including IE.
Example: Define an empty table as follows
<table id="myTable">
<tbody id="myTableBody"></ tbody>
</table>
The correct way to add rows to this table is to add rows to the table body, not to the table.
Var cell = document.createElement("td").appendChild(document.createTextNode("foo"));
Var row = document.createElement("tr").appendChild(cell);
Document.getElementById("myTableBody"). appendChild(row);
*In IE, you need to create rows first, then create columns, and then create content.
2. Set the style of the element.
Var spanElement = document.getElementById("mySpan");
//The following writing method is guaranteed to work in all browsers except IE. Available
spanElement.setAttribute("style","font-weight:bold;color:red;");
//The following writing method ensures that IE can use
spanElement.style.cssText="font-weight:bold;color:red;";
3. Set the class attribute of the element
Var element = document.getElementById(“myElement”);
//The following writing method ensures that all browsers except IE can use
Element.setAttribute(“class”,”styleClass”);
//The following writing method Ensure that IE is available in
Element.setAttribute("className","styleClass");
4. Create input elements
Var button = document.createElement("input");
//Single-line text box, check box, radio button, radio button , Buttons need this attribute to distinguish
Button.setAttribute("type","button");
Document.getElementById("formElement").appendChild(button);
5. Add an event handler to the input element
Var formElement=document.getElementById(" formElement");
// Available in all browsers
formElement.onclick=function(){doFoo();};
// Available in all browsers except IE
formElement.setAttribute("onclick","doFoo();");
6. Create a radio buttonIf
(document.uniqueID){
//Internet Explorer
Var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'>");
}else{
/ /Standards Compliant
Var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value"," checked”);
}
7. insertRow, insertCell, deleteRow
In IE, if table.insertRow() does not specify parameters, rows will be added after the table. The default parameter bit is -1; if in Firefox, parameters must be added. Such as: insertRow(-1).