When Element add attribute by the method "setAttribute",It can't work in IE

来源:互联网 发布:阳光网络伴我成长图片 编辑:程序博客网 时间:2024/06/10 08:52

今天使用Javascript 操纵页面元素,想给指定的 Element 添加一些Attribute,使用

Ele.setAttribute("class", "tdbg");

Ele.setAttribute("onclick", "test");

结果在IE里面查看 元素的 Text 看到代码里面明明有了这些属性的设置 怎么一个都没起作用,但是在 mozilla里面却是对的,查看了相关文档,

原来 两句话在 IE 里面都错了,但是错的不一样。

第一句错的地方是 在IE里面 class 应为className

第二句是因为 在IE里面 onclick 是一个 Event,不能用添加 Attribute 的方法 setAttribute 来设定,

有两种方法可以使用:

1.使用DOM的 添加 Event 的方法,  Ele.attachEvent('ondblclick',testonclick);

2.elmt.onXXX = function() { return false; }

通过这两种方法都可以添加 Element 的事件,在 Mozilla 中也可正常使用

注意: row.attachEvent('ondblclick',testonclick);里面的事件都应为小写 如"ondblclick'" 像 "onDblClick" 这样是不对的。

参考文章:

"setAttribute does not work when used with the style attribute " at www.quirksmode.org

原创粉丝点击