2017-10-04 48 views
0

嗨,大家好我需要解決這個問題我有更多<a> attr。並在不同的點擊時,我會得到相同的網頁我想想和問題是jQuery代碼HREF問題無法修復它

var socialIcons = $(".social a"), 
    href = socialIcons.attr("href"); 

socialIcons.on("click",function(e){ 
    e.preventDefault(); 

    setTimeout(function() { 
      window.location.href = href; 
    }, 500); 
}); 
+0

你有多少'.social a'? – Huangism

+0

我有5個鏈接.social a – user8722409

+0

這就是爲什麼它不工作,看到由Barmar – Huangism

回答

2

你設置href到選擇加載頁面時相匹配的第一個元素的屬性,不是用戶實際點擊過的那個。您需要在回調函數中設置變量,並使其與被單擊的元素(處理函數中的this)相關。

socialIcons.on("click",function(e){ 
    e.preventDefault(); 
    var href = $(this).attr("href"); 
    setTimeout(function() { 
     window.location.href = href; 
    }, 500); 
}); 
+0

答覆非常感謝,現在它的工作,並在HTML代碼目標設置爲_blank,但現在是窗口打開在同一頁上如何修復它在jQuery中? thx – user8722409

+0

使用'window.open()'而不是'window.location.href =' – Barmar