2013-10-10 14 views
0

我使用這個腳本來強制鏈接在新窗口中使用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(); 
}); 
}; 

任何幫助極大的讚賞。

回答

1

addExternalLinks只是一個函數,而不是String的方法(這是什麼.html返回),也不是一個jQuery方法被鏈接。

$("#layerinfo").load(infoContent, function() { 
    addExternalLinks(); 
}); 

順便說一句,對於addExternalLinks不能你只需要添加.attr("target", "_blank")到所述鏈接,而不是使用Click事件?

+0

它在本地工作,但是當我上傳到服務器時,外部鏈接對加載的內容無效。有任何想法嗎 ?我修改了功能,根據你的消化: - – naturelab

+0

//添加外部鏈接 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

1

嘗試添加ATTR代替:

$(this).attr("target", "_blank"); 

希望這有助於!