Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/85.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将jQuery.load()和ajax dataFilter()结合起来_Javascript_Jquery_Ajax - Fatal编程技术网

Javascript 将jQuery.load()和ajax dataFilter()结合起来

Javascript 将jQuery.load()和ajax dataFilter()结合起来,javascript,jquery,ajax,Javascript,Jquery,Ajax,我使用jQuery.load()将html页面的内容加载到lightbox中。load在这个实例中做的最有用的事情是将完整的html页面转换成一个干净的html片段插入页面中 但是(由于第三方API有缺陷,动态加载时无法工作),我需要首先使用正则表达式过滤掉页面中的一到两个元素,然后再将其处理为html,这意味着我需要使用$.ajax的dataFilter选项 因此,现在我使用$.ajax而不是.load,我需要将过滤后的文本转换为.load自动提供的干净html 但是$(response)生成

我使用jQuery.load()将html页面的内容加载到lightbox中。load在这个实例中做的最有用的事情是将完整的html页面转换成一个干净的html片段插入页面中

但是(由于第三方API有缺陷,动态加载时无法工作),我需要首先使用正则表达式过滤掉页面中的一到两个元素,然后再将其处理为html,这意味着我需要使用$.ajax的dataFilter选项

因此,现在我使用$.ajax而不是.load,我需要将过滤后的文本转换为.load自动提供的干净html

但是$(response)生成了一个奇怪的错误jQuery对象,其中.find()、children()等。。。不要工作

有人能告诉我如何获得所需的干净html吗(我注意到injQuery的ajax代码已从V1.4.4版更改为1.5版,使用任何一个版本的解决方案都可以)

以下是我到目前为止所做的(使用jquery1.4.4)(所有引用的变量和方法都是在代码上面定义的)


嗯,我知道这是问了一段时间,但希望这仍然会对你有帮助。从您的问题中,我了解到您希望用$.ajax函数来复制$.load函数

您可能会发现未来使用的一个有用工具是,这样您就可以确切地看到jQuery是如何在幕后工作的

    // Using jquery 1.5.1
    $(function () {

        var rscript = "/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi";

        // Will override all ajax requests on this page including load
        $.ajaxSetup({
            dataFilter: function (response) {
                return response.replace("Lorem ipsum.", "");
            },
            type: "GET",
            dataType: "html",
            // Disable caching of AJAX responses for example only
            cache: false
        });

        $("#load").click(function (evt) {
            evt.preventDefault();
            $('#content').load("html.htm #lightBoxForm");
        });

        $("#ajax").click(function (evt) {
            evt.preventDefault();

            $.ajax({
                url: "html.htm",
                success: function (res) {
                    $("#content").html("#lightBoxForm" ?
                            $("<div>").append(
                                        res.replace(rscript, ""))
                                           .find("#lightBoxForm") : res);
                }
            });

        });

    });
//使用jquery 1.5.1
$(函数(){

var rscript=“/你能举个例子说明你正在加载什么、过滤什么以及你想插入什么吗?事实证明,我看到的bug与load/ajax无关,但感谢你为此付出的努力
    // Using jquery 1.5.1
    $(function () {

        var rscript = "/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi";

        // Will override all ajax requests on this page including load
        $.ajaxSetup({
            dataFilter: function (response) {
                return response.replace("Lorem ipsum.", "");
            },
            type: "GET",
            dataType: "html",
            // Disable caching of AJAX responses for example only
            cache: false
        });

        $("#load").click(function (evt) {
            evt.preventDefault();
            $('#content').load("html.htm #lightBoxForm");
        });

        $("#ajax").click(function (evt) {
            evt.preventDefault();

            $.ajax({
                url: "html.htm",
                success: function (res) {
                    $("#content").html("#lightBoxForm" ?
                            $("<div>").append(
                                        res.replace(rscript, ""))
                                           .find("#lightBoxForm") : res);
                }
            });

        });

    });
   <input type="button" id="load" value="Load" />
   <input type="button" id="ajax" value="Ajax" />

   <div id="content"></div>