2010-08-09 54 views
0

我的问题是DELETE ITEM(已动态添加的项目)在IE6中没有通过单击进行删除。在IE6中删除动态添加的DOM元素

的javascript:

var TDCount = 3; 
var i=0; 
function insertTD(){ 
     var possition=document.getElementById('elmnt_pos').value; 
     if(possition=="") 
     { 
        possition='a'; 
        alert('Enter a number!!!'); 
     } 
     if(isNaN(possition)) 
     { 
      alert('Enter a number!!!'); 
      document.getElementById('elmnt_pos').value=''; 
     }else{ 
var newTD = document.createElement("li"); 
     var newid='li'+TDCount++; 
      newTD.setAttribute("id", newid); 
      newTD.setAttribute("onclick","javascript:removenode(this);"); 
var newText = document.createTextNode(i+"New fruit " + (possition++)); 
newTD.appendChild(newText); 

var trElm = document.getElementById("menu"); 
var refTD = trElm.getElementsByTagName("li").item(possition-2); 
trElm.insertBefore(newTD,refTD); 
     i++; 
     } 

} 
function removenode(th) 
{ 
answer = confirm("Do you really want to Remove Element "+th.id + " ? ") 
     if (answer !=0) 
     { 
     document.getElementById('menu').removeChild(document.getElementById(th.id)); 
     } 

} 

HTML

<ul id="menu"> 
    <li id="li0" onclick="javascript:removenode(this);">apple</li> 
    <li id="li1" onclick="javascript:removenode(this);">Banana</li> 
    <li id="li2" onclick="javascript:removenode(this);">Jackfruit</li> 
</ul> 
<form name="justfrm"> 
    <input type="text" value="Enter the position" name="pos1" id="elmnt_pos" /> 
    <input type="button" value="click" onclick="javascript:insertTD()"/> 
</form> 

“输入位置” 是指在某个特定位置添加元素像2,3,5等 我们可以通过点击项目删除项目。

+0

请使用代码按钮(带二进制数字的代码),以便代码看起来不可怕 – 2010-08-09 12:08:46

+0

请尝试编辑您的代码示例,使其仅包含相关部分。并做一些格式化 - 这是伤害了我的眼睛。 – Skilldrick 2010-08-09 12:11:19

回答

0

我没有在Internet Explorer 6的实例来进行测试,但更可能是这条线,这是造成问题:

newTD.setAttribute("onclick","javascript:removenode(this);"); 

它不会在Internet Explorer 6你工作需要做的是这样的:

newTD.onclick = function() { removeNode(this); }; 

newTD.onclick = new Function("removenode(this)"); 

看到这个article获取更多信息。另外,作为一个便笺,你可能想使用像jQuery这样的库,它已经处理了这些类型的跨浏览器问题。

+0

非常感谢你的信息。我只是更换了 newTD.setAttribute(“onclick”,“javascript:removenode(this);”); BY newTD.onclick = new Function(“removenode(this)”); 它的工作很完美... 非常感谢。 – JAMES 2010-08-10 05:30:24