特色栏目

ASP源码

PHP源码

.NET源码

JSP源码

游戏频道
专题合集
关闭菜单
首页> Javascript教程> 开发跨浏览器JavaScript时要注意的问题

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

时间:2009-06-09 20:23:48 作者:互联网

1、          向表中追加行

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

例:定义如下一个空表

     

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

Var cell = do***ent.createElement(“td”).appendChild(do***ent.createTextNode(“foo”));

Var row = do***ent.createElement(“tr”).appendChild(cell);

Do***ent.getElementById(“myTableBody”).appendChild(row);

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

2、          设置元素的样式

Var spanElement = do***ent.getElementById(“mySpan”);

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

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

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

sp***lement.style.cssText=”font-weight:bold;color:red;”;

3、          设置元素的class属性

Var element = do***ent.getElementById(“myElement”);

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

El***nt.setAttribute(“class”,”styleClass”);

//下面写法保证IE可用

El***nt.setAttribute(“className”,”styleClass”);

4、          创建输入元素

Var button = do***ent.createElement(“input”);

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

Bu***n.setAttribute(“type”,”button”);

Do***ent.getElementById(“formElement”).appendChild(button);

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

Var formElement=do***ent.getElementById(“formElement”);

//所有浏览器可用

fo***lement.onclick=function(){doFoo();};

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

formEl***nt.setAttribute(“onclick”,”doFoo();”);

6、          创建单选钮

If(do***ent.uniqueID){

      //Internet Explorer

      Var radioButton=do***ent.createElement(“”);

}else{

      //Standards Compliant

      Var radioButton=do***ent.createElement(“input”);

      radioBu***n.setAttribute(“type”,”radio”);

      radioBu***n.setAttribute(“name”,”radioButton”);

      radioBu***n.setAttribute(“value”,”checked”);

}

7、          insertRow,insertCell,deleteRow

在IE中,ta***.insertRow()如果没有指定参数,则在表格后面添加行,默认参数位-1;如果在Firefox中,则一定要加参数,如:insertRow(-1)。

相关文章 最新文章

相关应用

热门文章

猜你喜欢

返回顶部