[英]How can I disable Javascript click function if link starts with #
How can I run this code on all links URL except links that start with #
like bootstrap collapse, modal links etc.?我如何在所有链接 URL 上运行此代码,除了以#
开头的链接,如引导崩溃、模式链接等?
$(function(){
$("a").click(function() {
$(".se-pre-con").fadeOut("slow");
$(this).after("<div class='se-pre-con text-center pp500'><i class='fa fa-list fa-spin fa-5x fa-fw text-color1'></i></div>").fadeIn();
});
});
You can simply check the first character to make sure it isn't #.您可以简单地检查第一个字符以确保它不是#。
$(function() {
$("a").click(function() {
if ($(this).attr("href").substr(0, 1) != "#") {
$(".se-pre-con").fadeOut("slow");
$(this).after("<div class='se-pre-con text-center pp500'><i class='fa fa-list fa-spin fa-5x fa-fw text-color1'></i></div>").fadeIn();
}
});
});
You can use the not
selector in jquery.您可以在 jquery 中使用not
选择器。 Example:例子:
$(function(){ $("a:not([href='#'])").click(function(e){ e.preventDefault(); alert("You clicked me;"); }); });
a { display: block }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <a href="link">Click Me!</a> <a href="link">Click Me Also!</a> <a href="link">Me Too!</a> <a href="#">Don't CLick me please!</a> <a href="#">Don't Click me!</a>
note: preventDefault stops the link from opening注意: preventDefault 会阻止链接打开
Just use CSS negation and prefix attribute selector:只需使用 CSS 否定和前缀属性选择器:
$('a:not([href^="#"])').click(...)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.