使用Internet Explorer 9单击元素时未调用`onclick`事件

Dej*_*ell 0 html javascript javascript-events internet-explorer-9

解决方案不适用于IE9.

我有以下代码:

$(document).ready(function() {
      document.getElementById('unsubref').onclick = function() {unsubcribe();}; 
});
Run Code Online (Sandbox Code Playgroud)

和unsubref是:

<div id="unsub">
<h1>Click <a id="unsubref">here</a> to unsubscribe from the mailing service</h1>
</div>
Run Code Online (Sandbox Code Playgroud)

在Chrome中,它运行得很好.unsubscribe正在使用警报调用该函数.

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

Dar*_*rov 5

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

$(document).ready(function() {
    $('#unsubref').click(unsubcribe); 
});
Run Code Online (Sandbox Code Playgroud)

或使用匿名函数:

$(document).ready(function() {
    $('#unsubref').click(function() {
        alert('unsubscribing ...');
    }); 
});
Run Code Online (Sandbox Code Playgroud)