Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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/2/batch-file/6.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 如何切换结构以确定具有主体的类?_Javascript_Jquery_Html_Css - Fatal编程技术网

Javascript 如何切换结构以确定具有主体的类?

Javascript 如何切换结构以确定具有主体的类?,javascript,jquery,html,css,Javascript,Jquery,Html,Css,我有这个样本: 代码HTML: <div class="body"> <div class="test">TEST</div> </div> switch (n) { //what I need to write instead of n case body: alert("my class is body"); break; case another_class:

我有这个样本:

代码HTML:

<div class="body">
    <div class="test">TEST</div>
</div>
switch (n) {   //what I need  to write instead of n
    case body:
        alert("my class is body");
        break;
    case another_class:
        alert("my class is another");
        break;
    default:
        //etc
}
我只想测试一下我的班级是否。。。一次一个类,使用开关结构

我在网站上的页面在正文中有不同的类,只有在有特定类的情况下才想应用一种样式

如果我不试图解释其他的话,我希望你理解我的意思


提前谢谢

您可以在jquery中这样做:

if($('.body').length){
    alert('this is body');
}
else if ($('.another_class').length)
{
    alert('another');
}
您可以使用以下选项:

switch (document.querySelector('div').classList[0]) {
    case "body":
        alert("my class is body");
        break;
    case "another_class":
        alert("my class is another");
        break;
    default:
        //etc
}


这可能会有所帮助。

我不是舒尔,但如果父元素的类不同,则可以在不使用任何javascript的情况下向子元素添加特定样式,只需按如下方式声明css:

.body .test{
   styling of test inside .body in any case, overruled by anything that follows ;)
}
.body.classNameA .test{
   styling of test in case of classNameA;
}
.body.classNameB .test{
   styling of test in case of classNameB;
}

如果.body类是正在更改的对象,请将其忽略。

当任何元素包含
body
类时,是否要显示其他类名?我要检查类的body。。。然后应用样式,如果没有,请执行此操作anything@Marius:检查哪里??类似于?
.body .test{
   styling of test inside .body in any case, overruled by anything that follows ;)
}
.body.classNameA .test{
   styling of test in case of classNameA;
}
.body.classNameB .test{
   styling of test in case of classNameB;
}