简体   繁体   English

如果链接以 #

[英]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.

相关问题 如何编写点击后运行javascript函数的不可点击链接? - How can I write a non-clickable link that runs a javascript function upon click? 如何在 javascript 中临时禁用按钮或“单击”事件? - How can I temporarily disable a button or 'click' event in javascript? 如何禁用通过Loadrunner Web(单击和脚本)下载javascript? - How can I disable downloading javascript with Loadrunner Web (Click and Script)? 如何使用JavaScript捕获点击并将页面发送到链接? - How can I use javascript to capture a click and send the page to a link? 单击时如何激活链接,并停用 javascript 中的其他链接? - How can I active a link when click, and deactivate the others in javascript? 如何添加点击处理程序功能以在Vue中动态链接? - How i can add click handler function to dynamically link in Vue? 如何禁用javascript和css的链接? - How do I disable a link with javascript and css? 如何禁用 JavaScript 中的 href 链接? - How do I disable a href link in JavaScript? 如何在运行ajax时禁用单击功能,相同的单击会启动ajax运行,并且在完成ajax之后,重新启用单击功能 - how to disable the click function while ajax running, same click starts ajax running, and after the ajax is complete, re-enable the click function 单击链接如何调用javascript函数? - How to call javascript function on click of a link?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM