我正在寻找一个选项,可以在新窗口中自动打开从我的网站(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/