How to select a group of tags that do not have children of a certain type

jQuery is now at version 1.4! In addition to several new features and the "usual" rewrite of the code, all documentation has been revised in a truly more readable than before. impostato a _blank ! But we see the issue of this post, problem: add, using jQuery, a class of all the tags a that have attribute target set to _blank !
This we had already seen in Very short snippet: jQuery auto target blank link . The problem is that often the images have a link to a new window and add another image to the board is not very nice. To do so on that particular link is not applied to the class there are two ways:

Add a class

, non vogliamo mostrino l'immagine. The first method, which is true with any link, is to add a class, for example noexternal , to all those links that, despite containing the attribute target="_blank" , we do not show the image. In practice we want to make sure that jQuery not take them into consideration.

1
) . not ( '.noexternal' ) . addClass ( 'external' ) ; $ ('A [target = _blank]'.) Not ('. Noexternal'). AddClass ('external');

This meotdo, albeit effective, forces, however, to make into a link that we want to exclude the class noexternal :

1
2
3
"blank" href = "http://www.undolog.com" > Link esterno con immagine < / a > < a target = "blank" href = "http://www.undolog.com"> External link with picture </ a >

"blank" class = "noexternal" href = "http://www.appleside.it" > Link esterno senza immagine < / a > < a target = "blank" class = "noexternal" href = "http://www.appleside.it"> External link without image </ a >

Use a loop each ()

Alternatively, to disable the use of the class of all those external links that contain an image, we can use:

1
2
3
4
5
6
7
) . each ( $ ('A [target = _blank]: not (. Noexternal)'). Each (
function () {
this ; var me = this;
! ( $ ( me ) . children ( 'img' ) . length > 0 ) ) if (($ (me). children ('img'). length> 0))
. addClass ( 'external' ) ; $ (Me). AddClass ('external');
}
);

This performs the same function as the previous one with the addition of checks to see if the "first" son is an image. . If so, the class external is not added to the tag a .
It would be interesting to see if there is a method to do the same thing without using a "cycle" each() !

There are no comments for this post

Leave a comment

TAG XHTML PERMITS: CODE ENTRY:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL