开发跨浏览器JavaScript时要注意的问题zz

向表中追加行

定义table时使用tbody元素,以保证包括IE在内的所有浏览器可用

例:定义如下一个空表

[code]<table id=”myTable”>

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

</table>[/code]

向这个表中增加行的正确做法是,把行增加到表体,而不是增加到表。

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

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

Document.getElementById(“myTableBody”).appendChild(row);[/code]

*IE中需要先创建行,再创建列,再创建内容

2、 设置元素的样式

[code]Var spanElement = document.getElementById(“mySpan”);[/code]

//下面写法保证出IE外,所有浏览器可用

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

//下面的写法保证IE可用

[code]spanElement.style.cssText=”font-weight:bold;color:red;”;[/code]

3、 设置元素的class属性

[code]Var element = document.getElementById(“myElement”);[/code]

//下面的写法保证除IE外,所有浏览器可用

[code]Element.setAttribute(“class”,”styleClass”);[/code]

//下面写法保证IE可用

[code]Element.setAttribute(“className”,”styleClass”);[/code]

4、 创建输入元素

[code]Var button = document.createElement(“input”);[/code]

//单行文本框、复选框、单选框、单选钮、按钮需要此属性区别

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

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

5、 向输入元素增加事件处理程序

[code]Var formElement=document.getElementById(“formElement”);[/code]

//所有浏览器可用

formElement.οnclick=function(){doFoo();};

//除IE外,所有浏览器可用

[code]formElement.setAttribute(“onclick”,”doFoo();”);[/code]

6、 创建单选钮

[code]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”);

}[/code]

7、 insertRow,insertCell,deleteRow

在IE中,table.insertRow()如果没有指定参数,则在表格后面添加行,默认参数位-1;如果在Firefox中,则一定要加参数,如:insertRow(-1)。[code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值