开发者

deleting dynamically added DOM elements in IE6

开发者 https://www.devze.com 2023-01-10 23:34 出处:网络
My problem is that theDELETE ITEM (Item which has been added dynamically) is not deleteing by click in IE6.

My problem is that the DELETE ITEM (Item which has been added dynamically) is not deleteing by click in IE6.

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>

"Enter the position" means add element on a specific position like 2,3,5 etc. We can Delete Item by click on item .


I don't have an instance on Internet Explorer 6 to test with, but more than likely it's this line, which is causing the problem:

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

It does not work in Internet Explorer 6. You will need to do something like:

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

or

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

See this article for more information. Also, as a side note you may want to look into using a library like jQuery, which already handles these types of cross-browser issues.

0

精彩评论

暂无评论...
验证码 换一张
取 消