2013-03-18 42 views
1

,所以我想做一个部分淡出以下,像这样导航到当动画结束

<a class="fadelink" href="path/to/file">Hello</a> 

<script type="text/javascript"> 
     $("a.fadelink").click(function(e){ 
      e.preventDefault();   
      $("#content").fadeTo(400,0,function(){ 
       window.location.href = $(this).attr("href"); 

      }); 
     }); 
    </script> 

问题的链接前,jQuery的不断回到我“不确定”和404的重定向HREF。

+2

'this'代表'#content'而不是代码中的'a.fadelink'。 – cdmckay 2013-03-18 04:21:21

+1

你想要访问'a.fadelink'或'#content'的'href'属性吗? – 2013-03-18 04:22:33

+0

封闭是probs – 2013-03-18 04:22:53

回答

9

$(this)$(this).attr("href")指向$("#content")这是错误。移动到右侧范围:

$("a.fadelink").on("click", function(evt) { 

    evt.preventDefault(); 

    var goTo = $(this).attr("href");   // get href value of `this` anchor 

    $("#content").fadeTo(400, 0, function() { 
     window.location = goTo;    // Now you can use it 
    }); 

}); 
+1

可能发誓我在发布之前尝试过,也许我需要更多的咖啡因。谢谢一堆。 – 2013-03-18 05:13:59

+0

@RichardDenton也为我做了一个! – 2013-03-18 05:15:59

+0

请停止浪费CPU周期:'this.href'输入更少,速度更快(不是任何人都会注意到,但是您会感到更快乐,因为它们会干扰更少的电子)。 – RobG 2013-03-18 05:19:20

2

你指错thishref属性存在于锚点上,而不是#content元素。

$("a.fadelink").click(function(e){ 
    e.preventDefault(); 
    var that = this; 
    $("#content").fadeTo(400,0,function(){ 
     window.location.href = $(that).attr("href"); 

    }); 
});