[英]Forcing links loaded with Ajax to open in a new tab
我正在使用此脚本强制使用Jquery在新窗口中打开到Open的链接,并且工作正常
// add external links
function addExternalLinks () {
$("a[href*='http://']:not([href*='"+location.hostname.replace
("www.","")+"']), a.linkException").each(function() {
if($(this).find('img ').length == 0) {
$(this).click(function(event) {
event.preventDefault();
event.stopPropagation();
window.open(this.href, '_blank');
}).addClass('externalLink').attr("title", $(this).attr("title")+" - ( This link will open in a new window )");
}
});
}
但是,页面的一部分正在使用通过LOAD从外部HTML页面加载的内容。
function showInfo( info ) {
$("#layerinfo").load("descriptions.html #" + info );
};
我希望包含在此已加载内容中的链接也被强制在具有相同脚本的新寡妇中打开。 我无法使其正常工作。
就像是 :-
function showInfo( info ) {
var infoContent = "descriptions.html #" + info;
$("#layerinfo").load(infoContent,function(){
$("#layerinfo").html().addExternalLinks();
});
};
任何帮助,不胜感激。
addExternalLinks
只是一个函数,而不是String的方法( .html
返回的结果),也不是要链接的jQuery方法。
$("#layerinfo").load(infoContent, function () {
addExternalLinks();
});
顺便说一句,对于addExternalLinks
,您不能只是将.attr("target", "_blank")
到上述链接中,而不是使用click事件吗?
尝试添加attr代替:
$(this).attr("target", "_blank");
希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.