18910140161

JavaScript-如果具有类X的对象也具有类Y,则执行-堆栈溢出

顺晟科技

2022-10-18 13:15:27

140

尝试在<figure>与类.woocommerce-product-gallery__wrapper有另一个类-.test类是通过另一个JavaScript按钮放在那里的,该按钮用于切换类的输入和输出。所以这个事件需要继续倾听。

.test

我觉得这是一件超级简单的事情,作为一个JS新手,我正在做一些超级愚蠢的事情/错过了一些东西。


顺晟科技:

考虑以下.

if(document.querySelector('.woocommerce-product-gallery__wrapper').classList.contains('test')){
  console.log('Hello');
};

这将选择两个类(if($(".woocommerce-product-gallery__wrapper.test" ).length > 0){ console.log('Found Test Element'); } woocommerce-product-gallery__wrapper)的所有元素。

你也可以这样做。

woocommerce-product-gallery__wrapper

这将使用类if($(".woocommerce-product-gallery__wrapper.test" ).length > 0){ console.log('Found Test Element'); } 迭代每个元素,并检查它是否具有woocommerce-product-gallery__wrapper类。当您只想对这些元素执行操作时,这很有帮助。

您还可以像这样使用test

.filter()

有很多方法可以找到特定的元素。

尝试:

if($( ".woocommerce-product-gallery__wrapper" ).hasClass( "test" )){
console.log('Hello');
// I have the 'hidden' class
};
  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航