我使用這個腳本來強制鏈接在新窗口中使用jQuery開開,並能正常工作強制鏈接到一個新的標籤
// 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)");
}
});
}
然而,頁面的一部分使用內容從外部HTML頁面加載,使用LOAD。
function showInfo(info) {
$("#layerinfo").load("descriptions.html #" + info);
};
我希望包含在這個加載內容中的鏈接也被迫在一個新的窗口中使用相同的腳本打開。 我無法讓它正常工作。
喜歡的東西: -
function showInfo(info) {
var infoContent = "descriptions.html #" + info;
$("#layerinfo").load(infoContent,function(){
$("#layerinfo").html().addExternalLinks();
});
};
任何幫助極大的讚賞。
它在本地工作,但是當我上傳到服務器時,外部鏈接對加載的內容無效。有任何想法嗎 ?我修改了功能,根據你的消化: - – naturelab
//添加外部鏈接 function addExternalLinks(){ $(「a [href * ='http://']:not([href * ='」+ location .hostname.replace (「www。」,「」)+「']),a.linkException」)。each(function(){if($(this).find('img').length == 0){ $(this).attr(「target」,「_blank」).addClass('externalLink').attr(「title」,$(this).attr(「title」)+「 - 將在新窗口中打開)「); } }); } – naturelab