2017-10-05 41 views
0
$('div#content a').attr('target', 'forceToIframeWindow'); 

$(document).on("click", 'div#content a', function(){ 
var thisSrc = $(this).attr("href"); 
$(document.body).append(
$('<iframe />').attr({ 
    "name":"forceToIframeWindow", 
    "src":thisSrc 
})) 
}); 

DOM看起來像這樣不開放,在這裏是工作環節:如果你想防止某些鏈接在IFRAME

<p><a href="http://example.com" target="forceToIframeWindow"><img src="test.jpg"></a></p> 

回答

0

<p><a href="http://example.com" target="forceToIframeWindow">Example</a></p> 

和鏈接不工作鏈接的默認行爲應該使用preventDefault方法

$(document).on('click', 'div#content a', function(e){ 
    e.preventDefault(); // Stops the default execution of a link. Which normally is to open in a new window 
    var thisSrc = $(this).attr("href"); 
    $(document.body).append(
     $('<iframe />').attr({ 
      "name":"forceToIframeWindow", 
      "src":thisSrc 
     }) 
    ); 
}); 
+0

這可以工作。我發現只是增加了返回錯誤的作品。 –