简体   繁体   中英

A HREF issue can’t fix it

Hi guys I need fix this problem I have more <a> attr. and when click on different I'll get same webpage I think about that and issue is in jQuery code

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

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

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

You're setting href to the attribute of the first element that matches the selector when the page is loaded, not the one that the user has actually clicked on. You need to set the variable in the callback function, and make it related to the clicked element (which is this in the handler function).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM