preventDefault()不能在div onclick上工作,我試過preventDefault和stopImmediatePropagation也沒有任何作用,請看下面的示例。e.preventDefault()不能在div onclick上工作
<title></title>
<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$().ready(function() {
$("div").click(function (e) {
e.preventDefault();
//e.stopImmediatePropagation();
return false;
});
//$("div").bind("click", function (e) { e.stopImmediatePropagation(); return false; });
});
</script>
<div onclick="alert('1');">
Sample text
</div>
你怎麼知道它不工作,即你看到的是什麼默認行爲或你觀察到的祖先元素的事件? – alex 2011-04-22 06:50:17
您無法確保您的綁定活動將首先運行。你試過$('div')嗎?removeAttr('onclick')? – regilero 2011-04-22 07:44:46