2010十一月5
IE9 JS创建radio
为什么跟风的人介么多啊?IE9这种东西我还没有试过,就有用户在使了。然后,问题也就来了。
IE9可能更正规了一些,走的也是和平路线了。这点很值得鼓励,毕竟对自己之前的一些否定也需要很大的勇气才是。
IE JS 创建radio一直比较烦,因为和其他浏览器不统一。这回 IE9和IE8也不统一了。于是悲剧发生了。
function createRadio(name,value,isChecked)
{//{{{
var oRadio = null;
if(document.all)
{
var ieVer = getInternetExplorerVersion();
if(ieVer === 9){
return createRadioDef(name,value,isChecked);
}else{
return createRadioByIe(name,value,isChecked);
}
}
else
{
return createRadioDef(name,value,isChecked);
}
return oRadio;
}//}}}
function createRadioByIe(name,value,isChecked)
{//{{{
var oRadio;
oRadio = document.createElement("<input name='" + name + (isChecked ? "' checked='"+ isChecked +"'/>" : "' />"));
oRadio.type = "radio";
oRadio.value = value;
return oRadio;
}//}}}
function createRadioDef(name,value,isChecked)
{//{{{
var oRadio;
oRadio = document.createElement("input");
oRadio.setAttribute("type","radio");
oRadio.setAttribute("name",name);
oRadio.setAttribute("value",value);
if(isChecked)
{
oRadio.setAttribute("checked",isChecked);
}
return oRadio;
}//}}}
{//{{{
var oRadio = null;
if(document.all)
{
var ieVer = getInternetExplorerVersion();
if(ieVer === 9){
return createRadioDef(name,value,isChecked);
}else{
return createRadioByIe(name,value,isChecked);
}
}
else
{
return createRadioDef(name,value,isChecked);
}
return oRadio;
}//}}}
function createRadioByIe(name,value,isChecked)
{//{{{
var oRadio;
oRadio = document.createElement("<input name='" + name + (isChecked ? "' checked='"+ isChecked +"'/>" : "' />"));
oRadio.type = "radio";
oRadio.value = value;
return oRadio;
}//}}}
function createRadioDef(name,value,isChecked)
{//{{{
var oRadio;
oRadio = document.createElement("input");
oRadio.setAttribute("type","radio");
oRadio.setAttribute("name",name);
oRadio.setAttribute("value",value);
if(isChecked)
{
oRadio.setAttribute("checked",isChecked);
}
return oRadio;
}//}}}
文章作者:simaopig
本文地址:http://www.xiaoxiaozi.com/2010/11/05/1981/
版权所有 © 转载时必须以链接形式注明作者和原始出处!
这个不是和标准方法一样了么?可以和标准方法合并啊,先判断IE版本号,用一个&&,应该代码简洁一点速度快一点吧?
[回复]
另外我觉得这样hack效果并不好,考虑下下面的代码怎么样:
function createRadio(name,value,isChecked)
{//{{{
var oRadio = null;
try {
oRadio = document.createElement(“” : “‘ />”));
oRadio.type = “radio”;
oRadio.value = value;
}
catch(e) {
oRadio = document.createElement(“input”);
oRadio.setAttribute(“type”,”radio”);
oRadio.setAttribute(“name”,name);
oRadio.setAttribute(“value”,value);
if(isChecked) {
oRadio.setAttribute(“checked”,isChecked);
}
}
return oRadio;
}//}}}
[回复]
@ayanamist
呵呵,是这样的。我之所以不这样弄。是因为try catch 真正作用是抛出异常,而并不是if /else 的功能。
catch 本意是在出现错误的时候,抛出异常,进行一些处理。
如果通过 try catch 来兼容不同浏览器,感觉不是好正规的套路哦。呵。
[回复]
因为没有更好的hack方法。我觉得要针对特性做兼容,而不要针对浏览器做兼容。否则就会像你的代码那样写的很冗余一点都不dry,而且一旦出了新的浏览器就要去修改hack。
[回复]
不用IE的飘过。虽然我也升级了IE9 BETA( ̄_ ̄|||)
[回复]
@ayanamist
呵呵,确实有这个道理。不过没有好的方式,不代表应该这样搞啊。
其实我应该再完善一下,把所有IE的都写在一起,然后在IE的里面里再来写一些兼容性的问题。
而且,我在尝试,是否可以不采用判断浏览器版本的操作来处理。谢谢啦~
[回复]
@LAONB
呵呵。我还没有升级,不跟风。嗯。
[回复]
@simaopig
hack 本身就是不标准的东西,没有必要拿标准的东西去衡量的。我觉得是这样的。可以认为这是我的奇淫异技……
[回复]
@ayanamist
呵呵,好吧。拜读。哈哈。
[回复]