The following are some cross-browser js methods for implementing ajax. These methods can achieve the same effect in any browser.
I. append rows to the table
Use the tbody element when defining a table to ensure that all browsers including IE are available.
For example, define an empty table.
<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 instead of 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 must first create a CCB, then create a column, and then create content.
Ii. Set the element style
Var spanElement = document. getElementById ("mySpan ");
// The following statement ensures that all browsers are available outside IE
SpanElement. setAttribute ("style", "font-weight: bold; color: red ;");
// The following statement ensures IE availability
SpanElement.style.css Text = "font-weight: bold; color: red ;";
Therefore, you can write the above two sentences.
3. Set the class attribute of the element
VaR element = Document. getelementbyid ("myelement ");
// The following statement ensures that all browsers except IE are available
Element. setattribute ("class", "styleclass ");
// The following statement ensures ie availability
Element. setattribute ("classname", "styleclass ");
Therefore, you can write the above two sentences.
4. Create text input Elements
VaR button = Document. createelement ("input ");
// This attribute is required for single-line text boxes, check boxes, single-choice buttons, and buttons.
Button. setattribute ("type", "button ");
Document. getelementbyid ("formelement"). appendchild (button );
5. Add an event handler to the input element
VaR formelement = Document. getelementbyid ("formelement ");
// All browsers except IE are available
Formelement. setattribute ("onclick", "dofoo ();");
// All browsers are available
Formelement. onclick = function () {dofoo ();};
6. Create a radio button
The uniqueid object exists in IE browser.
If (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, table. if no parameter is specified for insertRow (), add a row to the end of the table. The default parameter bit is-1. If it is in Firefox, add a parameter, for example, insertRow (-1 ).