最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using jQuery to open all external links in a new window - Stack Overflow

programmeradmin5浏览0评论

New to jQuery here. I've found several web pages that e close to what I'm trying to do, but not quite. Actually, I think the following is supposed to actually work, but it's saying:

[@href^="http://"]

is not recognized (syntax error). Any help?

$(document).ready(function() {
    $('a[@href^="http://"]').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');  
});

Thanks.

New to jQuery here. I've found several web pages that e close to what I'm trying to do, but not quite. Actually, I think the following is supposed to actually work, but it's saying:

[@href^="http://"]

is not recognized (syntax error). Any help?

$(document).ready(function() {
    $('a[@href^="http://"]').filter(function() {
        return this.hostname && this.hostname !== location.hostname;
    }).attr('target', '_blank');  
});

Thanks.

Share Improve this question edited Dec 9, 2009 at 3:24 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked Dec 9, 2009 at 3:14 Jeff LambJeff Lamb 5,8754 gold badges39 silver badges57 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

No need for the @ symbol. Other than that, you're golden.

$("a[href^='http://']")...
发布评论

评论列表(0)

  1. 暂无评论