我正在尝试检测某个 CSS 类是否应用了背景图像。
我可以简单地执行以下操作来测试是否存在背景图像:
if ($('.vc_custom_1498122672544').css('background-image') != 'none') {
alert('There is a background image');
}
但是上面的类是动态生成的,并不总是为人所知。
附加了这个类的 div 也应用了一些其他类。所以我尝试了以下方法:
if ($('.vc_row-has-fill').css('background-image') != 'none') {
alert('There is a background image');
}
这不会返回任何内容,因为背景图像专门针对 .vc_custom_1498122672544
而不是 .vc_row-has-fill
的类。
我能做些什么来检测类 .vc_row-has-fill
的背景图像吗?
最佳答案
实际上,您可以检查一个类是否有另一个类。例如,具有类 .vc_row-has-fill
的 div hasClass .vc_custom_1498122672544
或不。如果是这样,那么您可以进一步检查类 .vc_custom_1498122672544
是否有 background-image
$('.vc_row-has-fill').each(function(){
if ($(this).hasClass('vc_custom_1498122672544')){
if ($('.vc_custom_1498122672544').css('background-image') != 'none') {
alert('There is a background image');
}
}
});
演示:https://jsfiddle.net/9y52ef4c/
我不确定这是否是您想要实现的目标。希望能有所帮助
关于jQuery 检测 css 类是否有背景图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695272/