Cross-browser JS methods for implementing Ajax

Source: Internet
Author: User
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 ).
 
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.