【问题标题】:`onclick` event not called when element clicked with Internet Explorer 9使用 Internet Explorer 9 单击元素时未调用“onclick”事件
【发布时间】:2012-01-18 10:16:17
【问题描述】:

This 解决方案不适用于 IE9。

我有以下代码:

$(document).ready(function() {
      document.getElementById('unsubref').onclick = function() {unsubcribe();}; 
});

而 unsubref 是:

<div id="unsub">
<h1>Click <a id="unsubref">here</a> to unsubscribe from the mailing service</h1>
</div>

在 Chrome 中,它工作得很好。函数 unsubscribe 正在被警报调用。

但是,在 IE9 中它不起作用!

【问题讨论】:

  • 贴出unsubscribe()函数的代码。
  • 在我的 IE (7-9) 中工作。我也不明白为什么,在使用 jQuery 时,你会想要使用属性事件处理程序甚至 getElementById?
  • @Yoshi - 我怎样才能使用别的东西呢?
  • 见@DarinDimitrov 的回答。 ;)

标签: javascript html jquery internet-explorer-9 jquery-events


【解决方案1】:
 $('#unsubref').bind("click", function(){ alert(''); });

【讨论】:

  • -1 DOM 元素上没有 bind 方法。可能在哪里考虑 jQuery?
【解决方案2】:

由于您使用的是 jQuery,我建议您使用 .click() 方法订阅处理程序:

$(document).ready(function() {
    $('#unsubref').click(unsubcribe); 
});

或使用匿名函数:

$(document).ready(function() {
    $('#unsubref').click(function() {
        alert('unsubscribing ...');
    }); 
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 2017-12-05
    • 2015-10-11
    • 1970-01-01
    • 2023-03-28
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多