jQuery add target=“_blank” for outgoing link

后端 未结 15 1774
滥情空心
滥情空心 2020-12-13 04:01

I need some help to create jquery script :)
I have some of link like this on my HTML.

Google


        
15条回答
  •  天涯浪人
    2020-12-13 04:54

    You could use jQuery's $.each function to iterate over all Anchor tags, perform the needed check and set the "target" attribute using $(this).attr("target","_blank");

    Example (Not tested but should work):

    $('a').each(function(index) {
        var link = $(this).attr("href");
        if(link.substring(0,7) == "http://")
            $(this).attr("target", "_blank");
    });
    

    Shai.

提交回复
热议问题