开发过程中,需要使用JS向页面动态添加radio,实现时通过document.createElement()方法来实现,刚开始的代码如下:

 

);   
_radio.type = "radio";   
_radio.name 
= "_radio";   
document.body.appendChild(_radio);   
_radio 
= document.createElement("input");   
_radio.type 
= "radio";   
_radio.name 
= "_radio";   
document.body.appendChild(_radio);  

结果在IE浏览器上生成的raido无法选中,而使用firefox浏览器却可以选中。后来试了下面的代码:

);   
document.body.appendChild(_radio);   
_radio = document.createElement("<input type='radio' name='_radio'>");   
document.body.appendChild(_radio);  

结果在IE上生成的radio可以选中了,但firefox浏览器中却无效。为此,对于不同的浏览器,需要使用不同的方式来生成radio,这个可以通过判断document是否具有uniqueID属性实现,因为uniqueID是IE特有的属性,为此可以通过下面代码来做到浏览器的兼容性:

 

(document.uniqueID) {   
    //IE浏览器分支   
    var _radio = document.createElement("<input type='radio' name='_radio'>");   
    document.body.appendChild(_radio);   
    _radio 
= document.createElement("<input type='radio' name='_radio'>");   
    document.body.appendChild(_radio);   
else {   
    
//非IE浏览器分支   
    var _radio = document.createElement("input");   
    _radio.type 
= "radio";   
    _radio.name 
= "_radio";   
    document.body.appendChild(_radio);   
    _radio 
= document.createElement("input");   
    _radio.type 
= "radio";   
    _radio.name 
= "_radio";   
    document.body.appendChild(_radio);   
}  

此外,在IE浏览中,通过document.createElement("input")来生成的radio和checkbox都无法通过document.getElementsByName()方法来获取。

摘:http://hotpepper.javaeye.com/blog/236338

相关文章:

  • 2022-12-23
  • 2021-11-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
  • 2022-12-23
  • 2022-01-29
猜你喜欢
  • 2022-12-23
  • 2021-11-20
  • 2022-12-23
  • 2021-08-02
  • 2021-06-20
  • 2021-12-21
  • 2021-07-08
相关资源
相似解决方案