[英]Why hyperlink not open in new tab?
I have 3 file html: force_hyperlink_open_in_new_window.html 我有3个文件html: force_hyperlink_open_in_new_window.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Force hyperlink open in new window</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
</head>
<body>
<a href="force_hyperlink_open_in_new_window_01.html">Foo</a>
<a href="force_hyperlink_open_in_new_window_02.html">Bar</a>
<script type="text/javascript">
$('a[href^="http://"]')
.not('[target="_blank"]')
.attr('target', '_blank');
</script>
</body>
</html>
force_hyperlink_open_in_new_window_01.html force_hyperlink_open_in_new_window_01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>01</title>
<style>
body{
background-color: tomato;
}
</style>
</head>
<body>
</body>
</html>
force_hyperlink_open_in_new_window_02.html force_hyperlink_open_in_new_window_02.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>02</title>
<style>
body{
background-color: limegreen;
}
</style>
</head>
<body>
</body>
</html>
Why 2 hyperlinks don't open in new tab (I use Google Chrome Version 52.0.2743.116 (64-bit))? 为什么2个超链接无法在新标签页中打开(我使用的是Google Chrome版本52.0.2743.116(64位))?
Could be because the hrefs don't start with http://. 可能是因为href并非以http://开头。 Also, you can force opening links in new tabs by putting this line in head:
另外,您可以通过将以下行置于开头来强制在新标签页中打开链接:
<base target="_blank" />
You select anchors starting with http://
while your links are relative and don't start with http://
. 您可以选择以
http://
开头的锚,而链接是相对的,而不是以http://
开头。
Change the JQuery selector to general anchors: $('a')
and it will add your target, if they don't already have it. 将JQuery选择器更改为常规锚点:
$('a')
,它将添加您的目标(如果他们还没有的话)。 See https://jsfiddle.net/k7ww81ee/ 参见https://jsfiddle.net/k7ww81ee/
$('a')
.not('[target="_blank"]')
.attr('target', '_blank');
Your href does not match they not have http. 您的href不匹配,因为他们没有http。
Change: 更改:
$('a[href^="http://"]')
to 至
$('a')
to grep all links. grep所有链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.