2011-12-12 86 views
0

This解決方案不適用於IE9。使用Internet Explorer 9單擊元素時未調用`onclick`事件

我有以下代碼:

$(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中它不起作用!

+0

後的代碼'退訂()'功能。 –

+0

適用於我的IE(7-9)。另外我不明白爲什麼在使用jQuery的時候,你會想要使用attribute-event-handlers或者甚至是'getElementById'? – Yoshi

+0

@Yoshi - 我該如何使用別的東西呢? – Dejell

回答

5

由於您使用jQuery的,我會建議你使用.click()方法訂閱處理程序:

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

或使用匿名函數:

$(document).ready(function() { 
    $('#unsubref').click(function() { 
     alert('unsubscribing ...'); 
    }); 
}); 
-1
$('#unsubref').bind("click", function(){ alert(''); }); 
+1

-1在DOM元素上沒有'bind'方法。在哪裏思考jQuery可能? – Craig

相關問題