-1

有没有办法使用 jQuery 仅将外部 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); 
        }
    });

});
4

1 回答 1

0

利用.load

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

在文件名后包含一个空格,其中包含您希望从该文件定位的特定内容。接口:http ://api.jquery.com/load/

于 2013-10-28T19:46:25.607 回答