html - 自动在新窗口中打开每个外部链接(html、css)

标签 html css wordpress hyperlink external-links

我正在寻找一个选项,可以在新窗口中自动打开从我的网站(wordpress-blog)到任何其他网站的每个外部链接。使用 css 或 html 是否可以实现这一点,而无需手动将其作为“target _blank”手动执行 1000 次?

非常感谢!

PS:抱歉我的英语不好,我不是母语人士:(

最佳答案

将此代码放入主题的functions.php 文件中。

function cdx_handel_external_links() {
    ?>
<script type="text/javascript">
( function( $ ) {

    $("a[href^=http]").click(function(){
      if(this.href.indexOf(location.hostname) == -1) {
         $(this).attr({
            target: "_blank"
         });
      }
    })

   //Add Nofollow
   $("a").each(function(){
    if(this.href.indexOf('nytimes.com') >0 ){
        $(this).attr({
            rel: "nofollow"
         });
    }
   });

} )( jQuery );
</script>
   <?php
}
add_filter( 'wp_footer', 'cdx_handel_external_links', 999);

关于html - 自动在新窗口中打开每个外部链接(html、css),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41874623/

相关文章:

javascript - 如何使用外部 AS 文件将 AS3 转换为 HTML5

php - 如何使用 PHP 在 li 标签之前添加 span 标签?

CSS 类名在生产/开发中以不同的顺序进行解释

html - 如何使用 CSS 分割不同颜色的圆

css - 元素不会移动

jquery - 如何在 WordPress 的动态 Bootstrap 导航栏菜单中选择特定列表项

javascript - 通过 jquery 添加类主体 tinymce

javascript - 图库中图像大小不规则

java - Selenium : Not able to click on WebElement

javascript - 读取文本,计算单词长度并统计类似的出现次数