为什么超链接无法在新标签页中打开?

致你

我有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

<!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

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>02</title>
    <style>
        body{
            background-color: limegreen;
        }
    </style>
</head>
<body>
</body>
</html>

为什么2个超链接无法在新标签页中打开(我使用的是Google Chrome版本52.0.2743.116(64位))?

x

您可以http://在链接相对的情况下选择以开头的锚,而不是http://

将JQuery选择器更改为常规锚点:$('a')如果目标没有,它将添加您的目标。参见https://jsfiddle.net/k7ww81ee/

$('a')
        .not('[target="_blank"]')
        .attr('target', '_blank');

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有标签的超链接不会在新标签页中打开

为什么带有 rel="noopener noreferrer" 的链接总是在新标签页中打开?

Wicket为什么在新标签页中打开链接时页面过期?

为什么Safari无法在新标签页中打开Google搜索结果?

phpBB在新标签页中打开链接

Vaadin在新标签页中打开链接

在新标签页中打开链接的按钮

pandoc:在新标签页中打开链接

在新标签页中打开链接

表单无法在新标签页中打开

新标签页超链接到PDF

在新标签页中打开超链接,所有内容均应仅使用HREF属性值编写

有什么办法可以从内部html(角度6)中的新标签页中打开链接

在新标签页中打开链接时,Firefox的HTTP引荐来源网址是什么?

如何使用JavaScript在新标签页中打开链接

绑定点击事件以在新标签页中打开链接

使用JQuery在新标签页中打开链接

在新标签页中打开[innerHTML]绑定的Angular 6链接

如何使按钮在JavaScript的新标签页中打开链接?

React-Router在新标签页中打开链接

从PDF文件在新标签页中打开链接

如何在HTML的新标签页中打开链接?

如果在新标签页中打开链接的语句

如何制作HTML链接以在Python的新标签页中打开

markdown链接在新标签页中打开

通知onclick-在新标签页中打开链接并集中

在新标签页中打开多个链接的宏

只有 CSS 在新标签页中打开链接

如何在NextJS的新标签页中打开链接?