2015-12-18 14 views
5

Đây là tệp ajax có vấn đề của tôi. Vui lòng tham khảo câu hỏi css ban đầu của tôi tại đây: .Current link styling on CSS only works on the 1st page and doesn't transfer to the otherAjax sẽ không cho phép kiểu liên kết hiện tại css hoạt động đúng cách

Làm cách nào để khắc phục mã Ajax này để bắt đầu cho phép tôi áp dụng kiểu dáng liên kết hiện tại của mình cho trang hiện tại thực tế?

$("a.ajax-link").live("click", function(){ 
$this = $(this); 
var link = $this.attr('href'); 
var current_url = $(location).attr('href'); 

if(link != current_url && link != '#') { 
    $.ajax({ 
     url:link, 
     processData:true, 
     dataType:'html', 
     success:function(data){ 
      document.title = $(data).filter('title').text(); 
      current_url = link; 
      if (typeof history.pushState != 'undefined') 
       history.pushState(data, 'Page', link); 

      setTimeout(function(){      
       $('#preloader').delay(50).fadeIn(600); 
       $('html, body').delay(1000).animate({ scrollTop: 0 },1000);      

       setTimeout(function(){ 

        $('#ajax-content').html($(data).filter('#ajax-content').html()); 
        $('#ajax-sidebar').html($(data).filter('#ajax-sidebar').html()); 

        $('body').waitForImages({ 
         finished: function() { 
          Website(); 
          backLoading(); 
          $('.opacity-nav').delay(50).fadeOut(600); 
         },          
         waitForAll: true 
        });        
       },1000); 
      },0); 
     } 
    }); 
} 
return false; 
}); 

Trả lời

3

Tôi tin rằng bạn chỉ cần loại bỏ các lớp current từ trước <li class = "current"> và cần phải áp dụng nó cho mẹ <li> thẻ hiện <a> thẻ của.

Điều này có thể được thực hiện theo cách sau:

$("a.ajax-link").live("click", function(){ 
    $this = $(this); 
    var link = $this.attr('href'); 
    var current_url = $(location).attr('href'); 

    if(link != current_url && link != '#') { 
     $.ajax({ 
      url:link, 
      processData:true, 
      dataType:'html', 
      success:function(data){ 

       //code to apply current class to current li 

       if($this.parent("div.logo").length == 0){ 
        $("li.current").removeClass("current"); 
        $this.parent("li").addClass("current"); 
       } 
       //code ends here 

       document.title = $(data).filter('title').text(); 
       current_url = link; 
       if (typeof history.pushState != 'undefined') 
        history.pushState(data, 'Page', link); 

       setTimeout(function(){      
        $('#preloader').delay(50).fadeIn(600); 
        $('html, body').delay(1000).animate({ scrollTop: 0 },1000);      

        setTimeout(function(){ 

         $('#ajax-content').html($(data).filter('#ajax-content').html()); 
         $('#ajax-sidebar').html($(data).filter('#ajax-sidebar').html()); 

         $('body').waitForImages({ 
          finished: function() { 
           Website(); 
           backLoading(); 
           $('.opacity-nav').delay(50).fadeOut(600); 
          },          
          waitForAll: true 
         });        
        },1000); 
       },0); 
      } 
     }); 
    } 
    return false; 
}); 
+0

Cảm ơn bạn rất nhiều! Nó đã làm việc! Làm thế nào tôi không thấy điều này ... – David

+0

Ah, câu hỏi tiếp theo! Cách này hoạt động là nó được kết nối với liên kết thực tế đúng không? Có cách nào để làm cho nó kiểm tra url thay thế? Bởi vì tôi cần trang chủ để duy trì hoạt động khi biểu tượng cũng được nhấp. Với mã này, kiểu dáng của trang chủ sẽ biến mất khi biểu trưng được nhấp. Bạn có hiểu vấn đề không? Hãy cho tôi biết nếu tôi cần phải xây dựng. Nhưng cảm ơn rất nhiều vì đã giúp tôi với mã số đó – David

+0

@David - cập nhật câu trả lời để tránh 'addClass' bất cứ khi nào liên kết logo được nhấp vào. Tôi hy vọng tôi hiểu bạn. – vijayP

Các vấn đề liên quan