[Reproduced] Develop cross-browser JavaScript should pay attention to the problem

2009-07-30  来源:本站原创  分类:Web  人气:261 

Reprinted】 【cross-browser JavaScript developers should pay attention to the issue 2008-07-20 09:36
Reprinted from: http://hi.baidu.com/mohaiguyan/blog/item/6654247faf140b0e28388aa0.html

1, to the table an additional line of

Tbody element in the definition table used in order to guarantee, including all browsers, including IE available

Example: is defined as an empty table

<table"myTable">

<tbody"myTableBody"> </ tbody>

</ table>

Additional rows to this table, the correct approach is to row to the table body, rather than to the table.

Var cell = document.createElement ( "td"). AppendChild (document.createTextNode ( "foo"));

Var row = document.createElement ( "tr"). AppendChild (cell);

Document.getElementById ( "myTableBody"). AppendChild (row);

* IE needs to first create a row, and then create the columns, and then create content

2, set the element style

Var spanElement = document.getElementById ( "mySpan");

/ / The following written assurance out of IE, all browsers are available

spanElement.setAttribute ( "style", "font-weight: bold; color: red;");

/ / IE can be used to ensure the following lines

spanElement.style.cssText = "font-weight: bold; color: red;";

3, set the element class attribute

Var element = document.getElementById ( "myElement");

/ / The following is written to ensure, inter IE, all browsers are available

Element.setAttribute ( "class", "styleClass");

/ / The following can be written to ensure IE

Element.setAttribute ( "className", "styleClass");

4, create input element

Var button = document.createElement ( "input");

/ / Single-line text box, check box, radio button, radio button, the button need to distinguish this property

Button.setAttribute ( "type", "button");

Document.getElementById ( "formElement"). AppendChild (button);

5, to increase the input element event handler

Var formElement = document.getElementById ( "formElement");

/ / All browsers are available

formElement.onclick = function () (doFoo ();};

/ / In addition to IE, all browsers are available

formElement.setAttribute ( "onclick", "doFoo ();");

6, create a radio button

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.insertRow () If you do not specify a parameter, then add a row behind the table, the default parameters of position -1; if the Firefox, be sure to add parameters such as: insertRow (-1).

相关文章