【问题标题】:Don't execute href jquery [duplicate]不要执行href jquery [重复]
【发布时间】:2012-08-30 23:52:54
【问题描述】:

可能重复:
Jquery if conditions false then prevent default

我有这段代码,它运行良好,但我希望它是动态的。

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow");         
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow", function () {
        window.location = getid+".php";
    });
});

我想先执行淡出,然后再使用 href 引导它。

我想要的是第一个

$(".nav a").click(function () {     
    var getid = $(this).attr('id');
    $("#background-wrapper").fadeOut("slow");
    $("#page_box").fadeOut("slow"); 
    $("header").fadeOut("slow");
    $("footer").fadeOut("slow");
});

然后触发这个

.nav a = href = "link"

【问题讨论】:

    标签: jquery


    【解决方案1】:

    您需要先使用event.preventDefault() 停止链接行为,然后手动重定向用户。试试这个:

    $(".nav a").click(function (e) {     
        e.preventDefault();
    
        var getid = $(this).attr('id');
        $("#background-wrapper").fadeOut("slow");
        $("#page_box").fadeOut("slow");         
        $("header").fadeOut("slow");
        $("footer").fadeOut("slow", function () {
            location.assign = getid + ".php";
        });
    });
    

    【讨论】:

      【解决方案2】:

      试试这个:

      $(".nav a").click(function (e) {     
      
          e.preventDefault(); /*  If this method is called, the default action of the event will not be triggered. e.g clicked anchors will not take the browser to a new URL */
      
          var pageUrl = this.id + ".php";
      
          $("#background-wrapper").fadeOut("slow");
          $("#page_box").fadeOut("slow"); 
          $("header").fadeOut("slow");
          $("footer").fadeOut("slow", function () {
              window.location.replace(pageUrl);
          });
      
      });
      

      【讨论】:

        【解决方案3】:

        试试这个:

        $(".nav a").click(function (e) {   
            e.preventDefault();
            var $ele = $(this);
            $("#background-wrapper, #page_box, header, footer").fadeOut("slow", function () {
                window.location = $ele.attr('id')+".php";
            });
        });
        

        【讨论】:

        • 谢谢!这个和我一起工作。我只是在最后删除了 .php,因为我希望它也有一个外部 url。再次感谢:)
        • 好东西!不要忘记接受你的回答!
        【解决方案4】:

        也许以下内容对您有用?

        $(".nav a").click(function (e) {     
            e.preventDefault();
            var pageUri = $(this).attr('id') + '.php';
            $("#background-wrapper, #page_box, header, footer").fadeOut('slow', function () {
                window.replace(pageUri)
            });
        }
        

        【讨论】:

          猜你喜欢
          • 2023-04-11
          • 2016-11-20
          • 1970-01-01
          • 2020-10-03
          • 2021-02-18
          • 2014-04-05
          • 2011-10-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多