samedi 26 avril 2014

JavaScript - n'arrivez img src à l'intérieur de la balise href - Stack Overflow


I can't seem to get the img src path from inside an href tag using the following code:


$(document).on('click', '.imagemodal', function() {

var imgsrc = $('img', this).attr("src");

alert("IMG: " + imgsrc);

});

My HTML looks like this:


<a href="#" class="imagemodal"><img src="/images/psmith/5/image.jpg" /></a>

Any idea what I'm missing?




You do not seem to be closing your anchor tag..


<a href="#" class="imagemodal"><img src="/images/psmith/5/image.jpg" /></a>

Check FIDDLE


Do you get any error in the page in the console section of the browser




check this one, it works: http://jsfiddle.net/xhhqU/1/


HTML:


<a href="#" class="imagemodal"><img src="http://www.gstatic.com/webp/gallery3/1.png" />

jQuery:


$(document).on('click', '.imagemodal', function() {
var imgsrc = $('img', this).attr("src");
alert("IMG: " + imgsrc);
});



What jQuery version do you use? If it is before 1.7 it does not work. With the later ones it does.


See info taken from jQuery docs page: "The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers."



I can't seem to get the img src path from inside an href tag using the following code:


$(document).on('click', '.imagemodal', function() {

var imgsrc = $('img', this).attr("src");

alert("IMG: " + imgsrc);

});

My HTML looks like this:


<a href="#" class="imagemodal"><img src="/images/psmith/5/image.jpg" /></a>

Any idea what I'm missing?



You do not seem to be closing your anchor tag..


<a href="#" class="imagemodal"><img src="/images/psmith/5/image.jpg" /></a>

Check FIDDLE


Do you get any error in the page in the console section of the browser



check this one, it works: http://jsfiddle.net/xhhqU/1/


HTML:


<a href="#" class="imagemodal"><img src="http://www.gstatic.com/webp/gallery3/1.png" />

jQuery:


$(document).on('click', '.imagemodal', function() {
var imgsrc = $('img', this).attr("src");
alert("IMG: " + imgsrc);
});


What jQuery version do you use? If it is before 1.7 it does not work. With the later ones it does.


See info taken from jQuery docs page: "The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers."


0 commentaires:

Enregistrer un commentaire