符合标准的等效于target ="_ blank"

asl*_*lum 5 html jquery strict new-window

有些情况下我必须在新窗口/选项卡中打开链接.有没有一种方法可以对严格的HTML有效?使用jQuery这样做是可以接受的,但我宁愿不只是将它隐藏target="_blank"在w/jQuery中,以便验证器不会看到它们.

Joh*_*hnP 6

既然你说jQuery是允许的.

<a href="http://mysite.com" class="newWindow">Open in new window</a>

$('a.newWindow').click(function(e){
   e.preventDefault();
   window.open(this.href);
});
Run Code Online (Sandbox Code Playgroud)

你也可以通过普通的JS来做到这一点.这样你的HTML就不会onclick遍布整个地方.

编辑 - e.preventDefault()根据Ian的建议更新使用.

  • `each` - >`click`?`return false` - >`e.preventDefault()`? (4认同)