国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網站 > WEB開發 > 正文

開發跨瀏覽器JavaScript時要注意的問題

2024-04-27 13:56:31
字體:
來源:轉載
供稿:網友

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)。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 贵德县| 永修县| 会同县| 老河口市| 广东省| 高台县| 林州市| 永胜县| 辉南县| 昌乐县| 安化县| 汶川县| 左云县| 克东县| 郑州市| 凤冈县| 监利县| 闵行区| 大同市| 阳谷县| 武义县| 铜鼓县| 庄浪县| 鄯善县| 泸溪县| 崇仁县| 望江县| 方城县| 黄浦区| 台前县| 浮山县| 巨鹿县| 遂溪县| 获嘉县| 利辛县| 丰顺县| 商城县| 洛宁县| 瑞金市| 永城市| 安平县|