在IE6中将onclick事件添加到li元素 [英] Adding onclick event to li element in IE6

查看:115
本文介绍了在IE6中将onclick事件添加到li元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个列表,我想通过JavaScript动态添加一个事件.我在Firefox中可以很好地运行它,但是在IE6(ugh)中也需要它,但是这是必需的.它不必很漂亮,只需要工作即可.触发的事件只是从列表中删除该项目.我不确定要使其正常工作需要做什么.这是到目前为止我所获得的一小部分.这些ID是唯一的,我只以其中一个为例.它在所有较新的浏览器中都很好用.

So I have a list, and I want to dynamically add an event via JavaScript. I have it working great in Firefox, but I also need it to work in IE6 (ugh), but it's required. It doesn't have to be pretty, just needs to work. The event that triggers simply removes the item from the list. I am not sure what I need to do to get it working. Here is small piece of what I have so far. The ids are unique, I just put that one in as an example. It works great in all newer browsers.

    var id = "123456";
    var list = document.createElement("ul");
    var listElement = document.createElement("li");
    listElement.setAttribute("id", id);
    listElement.setAttribute("onclick", "removeFromList('" + id + "')");
    listElement.appendChild(document.createTextNode(content));
    list.appendChild(listElement);
    document.getElementById('myElement').appendChild(list);

推荐答案

我没有IE6对此进行测试,但替换了onclick-line:

i don't have an IE6 to test this, but replacing the onclick-line:

listElement.setAttribute("onclick", "removeFromList('" + id + "')");

这可能有效:

listElement.onclick = function(){ removeFromList(id); };

您还可以使用 attachEvent 对于IE并坚持使用旧的解决方案(或更好地在 addEventListener 上使用)较新的.

you also could use attachEvent for IE and stick to you old solution (or better use addEventListener) on the newer ones.

这篇关于在IE6中将onclick事件添加到li元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆