如何使用最接近的j

时间:2018-05-29 12:46:13

标签: javascript css

我有一个简单的任务,让下一个被激活的选择器使条件工作,但它不起作用......为什么?

  const closestImgWrap = target.closest("[class^='imageWrap'] > img")

1 个答案:

答案 0 :(得分:2)

您的选择器正在查找另一个元素的子图像。它不是在寻找包含图像的包装元素。

如果您想要图像,那就是:

const closestImgWrap = target.closest("[class^='imageWrap']").find("> img")

如果您想要使用的包装器

const closestImgWrap = target.closest("[class^='imageWrap']:has('> img')")