`

IE setAttribute onclick 问题

阅读更多

Why does an onclick property set with setAttribute fail to work in IE?

 

Ran into this problem today, posting in case someone else has the same issue.

var execBtn = document.createElement('input');
execBtn.setAttribute("type", "button");
execBtn.setAttribute("id", "execBtn");
execBtn.setAttribute("value", "Execute");
execBtn.setAttribute("onclick", "runCommand();");
 

 

 

Turns out to get IE to run an onclick on a dynamically generated element, we can't use setAttribute. Instead, we need to set the onclick property on the object with an anonymous function wrapping the code we want to run.

 

execBtn.onclick = function() { runCommand() };
 

 

BAD IDEAS:

You can do

execBtn.setAttribute("onclick", function() { runCommand() });
 

 

but it will break in IE in non-standards mode according to @scunliffe.

 

You can't do this at all

execBtn.setAttribute("onclick", runCommand() );
 

 

because it executes immediately, and sets the result of runCommand() to be the onClick attribute value , nor can you do

execBtn.setAttribute("onclick", runCommand);
 

 

 

 

 


Or you could use jQuery and avoid all those issues:

var execBtn = $("<input>")
    .attr("type", "button")
    .attr("id", "execBtn")
    .attr("value", "Execute")
    .click(runCommand);
 

jQuery will take care of all the cross-browser issues as well.

 


分享到:
评论

相关推荐

    IE8的JavaScript点击事件(onclick)不兼容的解决方法

    博客园闪存分页是用JavaScript生成的,今天发现在IE8下点击页码不能翻页,翻页操作是在当前页码的...由于IE8不支持setAttribute方法,这里添加的onclick事件处理程序并未添加上。 后来改为jQuery的attr方法: 代码如

    JavaScript的setAttribute兼容性问题解决方法

    代码如下: var asubmit = document.... //在火狐中有效,而在ie中无效 代码如下: &lt;span xss=removed&gt; asubmit.setAttribute(“onclick”,”[removed]document.buyform.submit();”);&lt;/span&gt; //在ie中有效,火

    javascript setAttribute, getAttribute 在不同浏览器上的不同表现

    测试环境(客户端浏览器 ) IE6,IE7, IE8兼容模式, IE8 Firefox 3.6.8, google chrome 5.0.375.125 先来说明两个函数的标准定义。 elementNode.setAttribute(name,... 一、setAttribute的问题 elementNode为&lt;tr&gt;

    javascript 动态添加事件代码

    这里利用 setAttribute 指定 onclick 属性,简单,很好理解, 但是:IE 不支持,IE 并不是不支持 setAttribute 这个函数,而是不支持用 setAttribute 设置某些属性,包括对象属性、集合属性、事件属性,也就是说用 ...

    js 动态加载事件的几种方法总结

    有些时候需要动态加载javascript事件的一些方法往往我们需要在 JS 中动态添加事件,这就涉及到浏览器兼容性问题了...但是:IE 不支持,IE 并不是不支持 setAttribute 这个函数,而是不支持用 setAttribute 设置某些属性

    IE DOM实现存在的部分问题及解决方法

    在IE中脚本不能以setAttribute()来设置其样式信息(css),必须采用element.style.property=value(注:此处value为要设定的值)的方式来实现某个元素的呈现效果。 eg.document.getElementById(“id”).style....

    javascript实现爱你在FF IE下都有效的添加一个项目

    IE不是很乖,在IE里setAttribute只认一些静态属性,而像ONCLICK事件时无效的,要用attchEvent方法 无标题文档 [Ctrl+A 全选 注:如需引入外部Js需刷新才能执行]

    无限菜单之 xml+popup 版(IE5.5+)

    pops[degree].document.body.setAttribute("degree", degree); return pops[degree]; } CreatePopup(1); //创建一个2层的Popup家族 这个方法可以解决多个Popup共存的问题,只是如果要使用这个方法来实现...

    多种方法实现JS动态添加事件

    但是IE不支持用 setAttribute 设置某些属性,包括对象属性、集合属性、事件属性,也就是说用 setAttribute 设置 style、onclick、onmouseover 这些属性在 IE 中是行不通的。 方法二、用 attachEvent 和 ...

    三星9305收索

    &lt;!...--STATUS OK--&gt;&lt;html&gt;&lt;head&gt;;charset=utf-8"&gt;&lt;meta http-equiv="X-UA-Compatible" content="IE=Edge"&gt;&lt;link rel="dns-prefetch" href="//s1.bdstatic.com"/&gt;&lt;link rel="dns-prefetch" href="//t1.baidu....

Global site tag (gtag.js) - Google Analytics