IE中setAttribute的疑惑。

用javascript操纵页面元素,想给制定的Element添加Attribute,用了

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

    div.setAttribute("onclick", "test");
结果在IE里查看,看到元素的代码里明明有了这些设置,可是一个都没有起作用,用mozilla试是对的。
翻书…………
原来两句话在IE里都错啦。

IE里class应为className;

在IE里‘onclick’是一个Event,不能用添加Attribute 的方法 setAttribute 来设定,
后来选择了DOM的添加Event 的方法

div.attachEvent('onclick',test);

这种方法在mozilla中也可以正常使用。

posted on 2006-09-26 01:31 windy的木头 阅读(1491) 评论(2)  编辑 收藏 所属分类: JavaScript

评论

#1楼 2006-09-27 16:54 BoyLee      

'日付 フォーマット を チェック
Me.txt_date.Attributes.Add("onblur", "dealDate('" & Me.txt_date.ClientID & "')")

'登録ボタンを確認する
Me.btn_update.Attributes("onclick") = "return confirm('登録しますか?');"
  回复  引用  查看    

#2楼 2007-04-18 14:31 tim[未注册用户]

IE并不是不支持setAttribute这个函数,
而是不支持用setAttribute设置某些属性,例如对象属性、集合属性、事件属性,也就是说用setAttribute设置className ,style和onclick这些属性
在IE中是行不通的
为达到兼容各种浏览器的效果,可以用点符号法来设置Element的对象属性、集合属性和事件属性。document.getElementById("foo").className = "fruit";
document.getElementById("foo").style.cssText = "color: #00f;";
document.getElementById("foo").style.color = "#00f";
document.getElementById("foo").onclick= function () { alert("This is a test!"); }
  回复  引用    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 514757




相关文章:

相关链接:

导航

公告

统计

与我联系

搜索

 

留言簿

我的标签

随笔分类(3)

最新评论

阅读排行榜