B
B
Brahner2014-06-11 11:15:38
NoSQL
Brahner, 2014-06-11 11:15:38

How to add a class to a link containing an image?

I'm using Fancybox and adding the large class and rel="group" for the image links after the page has loaded.

$(".text a").addClass("large").attr('rel', 'group');

The problem is that if there are links in the text without pictures (to a third-party resource), they are also given a class and the rel attribute, and therefore following the link does not work.

How can I add the class and attribute I need only to links containing an image?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
L
lega, 2016-01-22
@lega

You can, the 3rd node (orbiter) is needed if you need to switch to the second node when the master falls.
> (in fact, it will be in the wings)
without the 3rd node, you will quickly / simply not switch to the slave

I
Ilya Lesnykh, 2014-06-11
@Brahner

For example, with filter :

$('a').filter(function( index ) {
    return $("img", this).length === 1;
})

Example .
Second option: using has :
Example .
The second option is more productive .

D
Denis Ineshin, 2014-06-11
@IonDen

1. Why do this in Javascript? This is not rational.
2. If you still need it for some reason, then regular expressions and the test function will help you:
a) get the href value of the link b) execute the test
function c) the regular expression must contain a check for .jpg, .jpeg, .png, .gif To learn how to write regular expressions, read this article first

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question