我正在努力追随, 1.从用户选择中使用Ajax获取响应,该选项返回单选按钮属性列表 2.一旦我获得属性列表,我将使用createElement()创建单选按钮 3.然后我将事件附加到点击按钮的单选按钮。 4.然后我使用appedChild
将元素添加到DOM以下是添加单选按钮(用于IE)的代码
var radio = '<input ';
radio += 'type ="radio" ';
radio += 'id="' + id + '" ';
radio += 'value="" ';
radio += '/>';
radioButton = document.createElement(radio);
radioButton.onClick = function(){alert('this is test')}
ele.appendChild(radioButton);
ele.innerHTML += 'none' + '<br/>';
现在所有这一切,当我向DOM添加单选按钮onclick不起作用,我不明白为什么?
全部谢谢
答案 0 :(得分:7)
您可以通过两种方式动态地将en元素添加到DOM:通过将html代码插入父元素或通过创建具有某些属性的子元素。在你的例子中,这两种方法是混合的 - 因此毫无意义。
方法1.插入HTML代码:
var radio = '<input type ="radio" id="' + id + '" value="" />';
ele.innerHTML = radio;
document.getElementById(id).onclick = function(){ alert('this is test'); };
方法2.创建DOM子项:
var radioButton = document.createElement('input');
radioButton.type = "radio";
radioButton.id = id;
radioButton.onclick = function(){ alert('this is test'); };
ele.appendChild(radioButton);
答案 1 :(得分:0)
该属性名为onclick
而不是onClick