使用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[^>]*>((.|[

])*)</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来引入了是不是 看官儿!!!

原文地址:https://www.cnblogs.com/yeminglong/p/12190905.html