jQuery is up to version 1.4! In addition to several new features and the "usual" rewrite of the code, all documentation has been revised so really more readable than before. impostato a _blank ! But we see the question of this post; problem: add, using jQuery, a class to all tags a possessing the attribute target set to _blank !
This we had already seen Very short snippet: jQuery auto target blank link . The issue is that the images often 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 valid with any link, is to add a class, for example noexternal , in all those links that, despite containing the attribute target="_blank" , we do not want show the image. In practice we want to make sure that jQuery does not take them into account.
1 | ) . not ( '.noexternal' ) . addClass ( 'external' ) ; $ ('A [target = _blank]'). Not ('. Noexternal'). AddClass ('external'); |
This meotdo, while efficient, however, forced to put in the link 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"> Link external image </ 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 an image </ a > |
Use a cycle 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 (($ (I). children ('img'). length> 0)) . addClass ( 'external' ) ; $ (I). AddClass ('external'); } ); |
This performs the same function as the old one with the addition of verifying whether the "first" child image. . If so, the class external to the tag is not added 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