Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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-如何获取元素';ajax请求中的s类_Javascript_Jquery_Html_Ajax_Wordpress - Fatal编程技术网

Javascript jQuery-如何获取元素';ajax请求中的s类

Javascript jQuery-如何获取元素';ajax请求中的s类,javascript,jquery,html,ajax,wordpress,Javascript,Jquery,Html,Ajax,Wordpress,通过替换main标记中的内容,我正在用ajax加载我的网站页面。 问题是,使用Wordpress,每个页面都有自己的body类,这些类对于样式设计非常有用,所以我想用下一个页面的类替换旧页面的body类 我想我应该运行一个新的ajax请求来获取整个html页面,然后检查body元素,然后使用.attr(“class”)获取类列表,最后用新的替换旧的body类… 但是类总是返回未定义的而不是类列表。 编辑:我尝试使用.cd main content而不是body,奇怪的是,我得到了这个元素的类。因

通过替换
main
标记中的内容,我正在用ajax加载我的网站页面。 问题是,使用Wordpress,每个页面都有自己的
body
类,这些类对于样式设计非常有用,所以我想用下一个页面的类替换旧页面的body类

我想我应该运行一个新的ajax请求来获取整个html页面,然后检查
body
元素,然后使用
.attr(“class”)
获取类列表,最后用新的替换旧的body类…

但是类总是返回未定义的
而不是类列表。

编辑:我尝试使用
.cd main content
而不是body,奇怪的是,我得到了这个元素的类。因此,我现在假设问题不是来自语法,而是来自元素本身。
我怎样才能让它在
body
元素上工作?(我已经尝试用
.filter
替换
.find
,但也不起作用。)

HTML结构

<body id="body" class="home page-id-number other-classes">
    <main>
        <div class="cd-main-content">
            <!-- inside is the dynamically loaded content-->
        </div>
    </main>
</body>

您的jQuery代码错误:

$.ajax({url: url, 
    success: function(data){
        var body = $(data).find("body"); //not #body
        var classes = body.attr("class");
        console.log(data); //returns the html as expected
        console.log("body : "+body); //returns [object Object]
        console.log("classes : "+classes); //returns undefined
    } 
});
右边的选择器是
body
而不是
#body
,它是一个id选择器

因此,要更改身体类别,请使用以下代码:

$.ajax({url: url, 
    success: function(data){
        var classes = $(data).find("body").attr("class"); //get the classes
        $("body").attr("class", classes); //set the classes
    } 
});

您的jQuery代码错误:

$.ajax({url: url, 
    success: function(data){
        var body = $(data).find("body"); //not #body
        var classes = body.attr("class");
        console.log(data); //returns the html as expected
        console.log("body : "+body); //returns [object Object]
        console.log("classes : "+classes); //returns undefined
    } 
});
右边的选择器是
body
而不是
#body
,它是一个id选择器

因此,要更改身体类别,请使用以下代码:

$.ajax({url: url, 
    success: function(data){
        var classes = $(data).find("body").attr("class"); //get the classes
        $("body").attr("class", classes); //set the classes
    } 
});

我找到了一个解决方案,虽然不理想,但它解决了问题

首先,我必须假设无法获取body元素的属性,因为它似乎是由jQuery过滤的。但是,可以获取子元素

最后,我将body类添加到
body
main
元素中(在wordpress的header.php文件中)

下面是我的代码:

HTML

<body id="body" class="home page-id-number other-classes">
    <main class="home page-id-number other-classes">
    <!-- if I had the possibility, I'd have added "data-body-class" instead of "class" -->
        <div class="cd-main-content">
            <!-- inside is the dynamically loaded content-->
        </div>
    </main>
</body>

我找到了一个解决方案,虽然不理想,但它解决了问题

首先,我必须假设无法获取body元素的属性,因为它似乎是由jQuery过滤的。但是,可以获取子元素

最后,我将body类添加到
body
main
元素中(在wordpress的header.php文件中)

下面是我的代码:

HTML

<body id="body" class="home page-id-number other-classes">
    <main class="home page-id-number other-classes">
    <!-- if I had the possibility, I'd have added "data-body-class" instead of "class" -->
        <div class="cd-main-content">
            <!-- inside is the dynamically loaded content-->
        </div>
    </main>
</body>
从字符串获取时,jQuery过滤“body”标记

因此,$(data)[0]将返回所有内容,而不返回正文

还要使用过滤器,而不是“查找” 所以你可以得到这样的课程:

$.ajax({url: url, 
    success: function(data){
       //replace body tag
       data = data.replace("<body", "<container").replace("body>", "container>");
       var classes = $(data).filter("container").attr("class");
        $("body").attr("class", classes);
    } 
});
$.ajax({url:url,
成功:功能(数据){
//更换车身标签
数据=数据。替换(“”);
var classes=$(数据).filter(“容器”).attr(“类”);
$(“body”).attr(“class”,class);
} 
});
从字符串中获取时,jQuery会过滤“body”标记

因此,$(data)[0]将返回所有内容,而不返回正文

还要使用过滤器,而不是“查找” 所以你可以得到这样的课程:

$.ajax({url: url, 
    success: function(data){
       //replace body tag
       data = data.replace("<body", "<container").replace("body>", "container>");
       var classes = $(data).filter("container").attr("class");
        $("body").attr("class", classes);
    } 
});
$.ajax({url:url,
成功:功能(数据){
//更换车身标签
数据=数据。替换(“”);
var classes=$(数据).filter(“容器”).attr(“类”);
$(“body”).attr(“class”,class);
} 
});

对不起,我编辑了代码以避免混淆。我的身体确实有一个身体的id,所以这不是问题的根源。感谢您提供的设置课程的提示!对不起,我编辑了代码以避免混淆。我的身体确实有一个身体的id,所以这不是问题的根源。感谢您提供的设置课程的提示!哦,更换身体标签是个好主意。实际上我更喜欢你的解决方案,因为它不需要修改html。非常感谢。哦,更换身体标签是个好主意。实际上我更喜欢你的解决方案,因为它不需要修改html。非常感谢。