Question about jquery poll

I did a run-in of images (at first I do without tutorials), and I want to know if there is a way to kill a script if one image ends with over.jpg and makes the script continue if the image is not

this is my code

$('#topNav a img').hover(function(){ var rSrc = $(this).attr('name'); $(this).attr('src','images/' + rSrc + 'over.jpg'); }, function(){ var rSrc = $(this).attr('name'); $(this).attr('src', 'images/' + rSrc + '.gif'); }); 

HTML

  <a href="index.html" class="nav"><img src="images/m_1over.jpg" name="m_1"/></a> <a href="aboutus.html" class="nav"><img src="images/m_2.gif" name="m_2"/></a> 

I tried adding

 if($('img[src$=.gif]') 

before the script, but it does not work ...

+4
source share
3 answers

How about this:

 $('#topNav a img').not("[src$=over.jpg]").hover(function(){ ... 

See http://api.jquery.com/not/

+2
source

You can use the not() method as follows:

 $('#topNav a img').not('#topNav a img[src=over.jpg]').hover(function(){...} 
+2
source

You mean you need something like this?

 $('#topNav a img').each(function(i, el) { if ($(el).attr('src').match('\\.gif$') == '.gif') { $(el).hover(function(){ var rSrc = $(this).attr('name'); $(this).attr('src','images/' + rSrc + 'over.jpg'); }, function(){ var rSrc = $(this).attr('name'); $(this).attr('src', 'images/' + rSrc + '.gif'); }); } } 

You can play with the selector here, where I checked the code: http://jsfiddle.net/Exceeder/NE3Ps/

0
source

Source: https://habr.com/ru/post/1335752/


All Articles