![](/img/trans.png)
[英]Is it possible to create a div element with jquery onclick of a link
[英]jQuery onClick on link element
假設我有一些像這樣的代碼:
<a href="www.foobar.com" class="special-click">
Content here
</a>
$( ".special-click" ).click(function(event) {
event.preventDefault();
// Do other stuff
});
假設加載了jQuery庫,我的jQuery是否保證在頁面轉換之前運行? 換句話說,是否保證在上面的例子中用戶永遠不會被帶到www.foobar.com? 如果沒有,是否有一種方法可以添加該行為而無需添加html onclick屬性?
如果你想保證,那么在$(document).ready(function(){})中阻止這個點擊事件代碼;
<html>
<head>
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
$(".special-click").click(function (event) {
event.preventDefault();
alert("test")
});
});
</script>
</head>
<body>
<a href="www.foobar.com" class="special-click">
Content here
</a>
</body>
</html>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.