网页A链接不是本站的加target="_blank"做打开新页面

javascript 文章 2020-11-30 15:08 590 0 全屏看文

AI助手支持GPT4.0

网页里很多时候会嵌入其他人的网页,但是点击的时候可能当前网页会替换成其他人的网页。

所有三叔写下这段代码,大家加到</body>前面的 <script>标签里即可。

$(function(){
	$("#detaildv").find("a").each(function(a){
		var href = $(this).attr("href");
		if(window.document.location.host.indexOf(href)==-1 && !$(this).attr("target")){
			$(this).attr("target","_blank");
			$(this).attr("rel","noreferrer");
		}
	})
})	


这里需要注意两个地方。

  1.   这个方法依赖jquery

  2. 代码种的detaildv请换成阁下的代码。


其他就没有了。

测试用例: http://www.ibilibili.com/

自己玩吧。

-EOF-

AI助手支持GPT4.0