在我的网络应用程序中,我需要在单击按钮时隐藏内容部分。 所以我的右上角有 5 个导航选项。但是他们中有 5 个使用相同的类名作为内容部分,因为所有这些内容都是在 cake php mvc 元素中动态生成的。因此,当我单击按钮时,我需要隐藏该内容部分,它适用于所有 4 个导航(类名:col1-9),除了一个(具有不同的类名:col1-2)。
在使用 jquery css 隐藏该部分之前,我尝试检查 html 上是否存在该类,但它只检查真实情况而不检查其他部分(类不存在)。
$('.notifications').click(function(){
if ($('.col1-9').length >0){
alert('different navigation'); //This is working for 4 navigation options
} else if ($('.col1-2').length){
alert('different g navigation'); //This is not working for remaining 1 navigation otion
}
});
最佳答案
将您的 if 语句更改为:
$('.col1-2').length <1
关于javascript - 如何识别类是否存在于html中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24103574/