如何在ie8中使用包含值的函数设置OnClick属性? [英] How to Set OnClick attribute with value containing function in ie8?

查看:62
本文介绍了如何在ie8中使用包含值的函数设置OnClick属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是更改链接的onclick属性.我可以成功完成,但是生成的链接在ie8中不起作用.确实可以在ff3中使用.

My goal is to change the onclick attribute of a link. I can do it successfully, but the resulting link doesn't work in ie8. It does work in ff3.

例如,这在Firefox 3中有效,但不适用于IE8.为什么?

For example, this works in Firefox 3, but not IE8. Why?

<p><a id="bar" href="#" onclick="temp()">click me</a></p>

<script>
    doIt = function() {
        alert('hello world!');
    }
    foo = document.getElementById("bar");
    foo.setAttribute("onclick","javascript:doIt();");
</script>

推荐答案

您不需要为此使用setAttribute-此代码有效(也适用于IE8)

You don't need to use setAttribute for that - This code works (IE8 also)

<div id="something" >Hello</div>
<script type="text/javascript" >
    (function() {
        document.getElementById("something").onclick = function() { 
            alert('hello'); 
        };
    })();
</script>

这篇关于如何在ie8中使用包含值的函数设置OnClick属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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