使用jquery的load方法只加載body之間的內容


 

使用jquery的load方法只加載body之間的內容。首先我們提取了$.fn.load.toString()得到了它的源碼,然后在源碼的基礎上做出修改。如下:

$.fn.load = function (a, b, c) {
        if ("string" != typeof a && bc) return bc.apply(this, arguments);
        var d, e, f, g = this, h = a.indexOf(" ");
        return h >= 0 && (d = $.trim(a.slice(h, a.length)), a = a.slice(0, h)), $.isFunction(b) ? (c = b, b = void 0) : b && "object" == typeof b && (f = "POST"), g.length > 0 && $.ajax({
            url: a,
            type: f,
            dataType: "html",
            data: b
        }).done(function (a) {
            // 提取body部分
            var pattern = /<body[^>]*>((.|[\n\r])*)<\/body>/im;
            var matches = pattern.exec(a);
            if (matches) {
                a = matches[1]
            }
            // 提取body部分
            e = arguments, g.html(d ? $("<div>").append($.parseHTML(a)).find(d) : a)
        }).complete(c && function (a, b) {
            g.each(c, e || [a.responseText, b, a])
        }), this
    }

 

 

 這樣我們就可以ajax加載另外一個界面的body之間的內容,為什么呢?因為一個HTML中只允許有一個body標簽,要不然就需要iframe來引入了是不是 看官兒!!!

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM