开发者

jQuery - Open all links in id in new window

Can anyone tell me of a way to open all links w开发者_JAVA百科ithin an id in a new window?


Put this in the head:

$(function () {
    $('#selector').attr('target', '_blank');
})


I'd do it like this:

$('#myId a').click(function() {
    window.open(this.href);
    return false;
});


Is it a single function that will open all the hyperlinks within an ID in new windows? That's what I thought. :P

$("#some_id a").each(function (i) {
    window.open(this.href);
  });


$('#id a').click(function() {
    this.target = "_blank";    
}


If you say "all links" ,that i understand that any of specific link (e.g. may be more than one on the page) should guide to blank page.

For this case, you may do all needed links,that guide to blank page ,with specific id and dynamically generated postfix, e.g.:

link_1, link_2 etc.

so the script will look like:

var linkId = "[id*=" + "link_]";
$(linkId).attr('target', '_blank');

here is regexp used.


Try this:

$('#myId').click(function(){
    this.target = "_blank";
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜