`
hackbomb
  • 浏览: 212943 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

开发跨浏览器的javascript

阅读更多

一、向表中追加行
在firefox/safari/opera中
document.createElement创建表单元格,再使用document.appendChild来将这些表单元格增加到表行
在ie中
使用tbody,把行增加到表体,而不是增加到表

二、通过javascript设置元素的样式
在firefox/safari/opera中
可以通过javascript使用元素的setAttribute方法设置元素的样式
在ie中
使用元素的style对象的cssText属性来设置所需的样式

可以同时使用上面2种方法来设置

三、设置元素的class属性
使用元素的setAttribute方法,将class和className都用做属性名

四、创建输入元素
创建输入元素后设置其所有属性,特别是type属性,然后再把它增加到父元素中
var button=document.createElement("input");
button.setAttribute("type","button");
document.getElementById("formElement").appendChild(button);

五、向输入元素增加事件处理程序
使用点记法来引用所需的事件处理程序
var formElement=document.getElementById("formElement");
formElement.onclick=function(){ doFoo() ; };

六、创建单选钮
除ie以外
var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value",""checked"");

在ie中
var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'");

兼容
if(document.uniqueID){
    var radioButton=document.createElement("<input type='radio' name='radioButton' value='checked'");
}else{
    var radioButton=document.createElement("input");
radioButton.setAttribute("type","radio");
radioButton.setAttribute("name","radioButton");
radioButton.setAttribute("value",""checked"");
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics