Pregunta

I validate image path whether it is image path or some other path. If it is the image then replace the particular href value to # But the # value is affected all links. How can i replace the image link to #

jQuery(document).ready(function($) {
   $("a").each(function(i, el) {
      var href_value = el.href;
      if (/\.(jpg|png|gif)$/.test(href_value)) {
         jQuery('a').prop('href', '#');
      }
   });
});

Here is the

FIDDLE

Any Suggestion would be great.

¿Fue útil?

Solución

use this

jQuery(this).prop('href', '#');

or

jQuery(el).prop('href', '#');

Otros consejos

Try with this like this

 jQuery(document).ready(function($) {
       $("a").each(function() {
            var href_value = $(this).attr('href');
            if (/\.(jpg|png|gif)$/.test(href_value)) {
               jQuery(this).prop('href', '#');
            } 
        });
  });

The reason all links get their href set to # is that you use jQuery('a').prop('href', '#'); when you find an image link. jQuery('a') finds all a-tags in the entire page.

Try using your el variable instead. Something like:

if (/\.(jpg|png|gif)$/.test(href_value)) {
   jQuery(el).prop('href', '#');
}

try the below code

el.href="#";

http://jsfiddle.net/Q3Zwh/3/

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top