![]() |
| 首页 > 编程 > ajax > JavaScript |
1
1楼 boogie 2006-10-10
在一个项目里需要用JavaScript动态建立radio buttons,然后替换span element下的内容,在IE里能显示出来,可就是无法做选择,郁闷了半天,后来查网上资料发现是IE的BUG,说是 "IE doesn’t allow the name attribute to be changed after the element is created“,可我试了下Checkbox就不存在此问题。
原代码: function createCxtjRadioButtons(data,cxtjId,cxtjTagId){
var tagCxtj = document.getElementById(cxtjTagId);
tagCxtj.removeChild(tagCxtj.childNodes[0]);
for (var key in data) {
var val = data[key];
var input = document.createElement("input");
input.setAttribute("name",cxtjId);
input.setAttribute("type","radio");
input.setAttribute("value",key);
tagCxtj.appendChild(input);
var label = document.createElement("label");
label.setAttribute("for",input_id);
label.appendChild(document.createTextNode(val));
tagCxtj.appendChild(label);
}
}
解决后: function createCxtjRadioButtons(data,cxtjId,cxtjTagId){
var tagCxtj = document.getElementById(cxtjTagId);
tagCxtj.removeChild(tagCxtj.childNodes[0]);
for (var key in data) {
var val = data[key];
var input = createElement("input", cxtjId);
input.setAttribute("type","radio");
input.setAttribute("value",key);
tagCxtj.appendChild(input);
var label = document.createElement("label");
label.setAttribute("for",input_id);
label.appendChild(document.createTextNode(val));
tagCxtj.appendChild(label);
}
}
function createElement(type, name) {
var element = null;
try {
// First try the IE way; if this fails then use the standard way
element = document.createElement('<'+type+' name="'+name+'">');
} catch (e) {
// Probably failed because we’re not running on IE
}
if (!element) {
element = document.createElement(type);
element.name = name;
}
return element;
}
参考: 1、http://www.thunderguy.com/semicolon/2005/05/23/setting-the-name-attribute-in-internet-explorer/ 2、http://alt-tag.com/blog/archives/2006/02/ie-dom-bugs/ 2楼 woogy 2007-09-17
搜索墙@2009 www.pkwall.com all rights reserved QQ:276471788 [京ICP备09111534号]
声明:本站部分数据来源于网络,仅供参考,如有版权问题,请联系我们,我们将及时删除!转载本站请注明来源
| |||||