J2EE跨瀏覽器開發

 

 

1、向表中追加行

 

定義table時使用tbody元素,以保證包括IE在內的所有瀏覽器可用

例:定義如下一個空表

<table id=”myTable”>

      <tbody id=”myTableBody”></tbody>

</table>

向這個表中增加行的正確做法是,把行增加到表體,而不是增加到表。

Var cell = document.createElement(“td”).appendChild(document.createTextNode(“foo”));

Var row = document.createElement(“tr”).appendChild(cell);

Document.getElementById(“myTableBody”).appendChild(row);

*IE中需要先創建行,再創建列,再創建內容

 

2、設置元素的樣式

 

Var spanElement = document.getElementById(“mySpan”);

//下面寫法保證出IE外,所有瀏覽器可用

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

//下面的寫法保證IE可用

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

 

3、設置元素的class屬性

 

Var element = document.getElementById(“myElement”);

//下面的寫法保證除IE外,所有瀏覽器可用

Element.setAttribute(“class”,”styleClass”);

//下面寫法保證IE可用

Element.setAttribute(“className”,”styleClass”);

 

4、創建輸入元素

 

Var button = document.createElement(“input”);

//單行文本框、複選框、單選框、單選鈕、按鈕需要此屬性區別

Button.setAttribute(“type”,”button”);

Document.getElementById(“formElement”).appendChild(button);

 

5、向輸入元素增加事件處理程序

 

Var formElement=document.getElementById(“formElement”);

//所有瀏覽器可用

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

//除IE外,所有瀏覽器可用

formElement.setAttribute(“onclick”,”doFoo();”);

 

6、創建單選鈕

 

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

 

在IE中,table.insertRow()如果沒有指定參數,則在表格後面添加行,默認參數位-1;如果在Firefox中,則一定要加參數,如:insertRow(-1)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章