![](/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.