2013-10-28 94 views
-1

有没有方法只使用jQuery将外部html文件的预定部分加载到div中?将html的部分加载到div中

如果外部HTML细分(如使用<div id="section1", "...2", "...3">为例)分成不同的部分,jQuery可以钓出div id =“section1”,并将其输出到目标div吗?

我可以调整此代码来实现吗?

$(document).ready(function() { 

    $.ajax({ 
     url : "sourceFile.html", 
     dataType : "html", 
     success : function (data) { 
      $("#targetDIV").html(data); 
     }, 
     error : function (XMLHttpRequest, textStatus, errorThrown) { 
      alert("doc did not load. Status: " + textStatus); 
      alert("doc did not load. Error: " + errorThrown); 
     } 
    }); 

}); 

回答

0

使用.load

$("#targetDIV").load("sourceFile.html #section1"); 

包括与您希望从该文件到目标的具体内容的文件名后面输入一个空格。 API:http://api.jquery.com/load/