jQuery selectors: only link with images inside the

2019-08-07 07:15发布

问题:

I want to open images with some kind of lightbox, colorbox for an example. How would it be possible to open only link which have images inside them and are in .content class?

Something like:

$('.content a img').colorbox();

回答1:

You can use the :has() selector:

$('.content a:has(img)').colorbox();

In case you already have a jQuery object $('.content a') containing all links you could also call .has('img') on it to reduce its elements just like the selector would do.