I'm trying to get the class name from an selection of images when clicked.
The code below retrieves the first images class name but when I click on my other images with different class values they display the first images class.
How can I get each images class value?
HTML
<a href="#" title="{title}" c开发者_开发百科lass="bg"><img src="{image:url:small}" alt="{title}" class="{image:url:large}" /></a>
jQuery Code
$(".bg").click(function(){
var1 = $('.bg img').attr('class');
Try this instead:
$(".bg").click(function(){
var1 = $(this).children('img').attr('class');
Try:
$(".bg").click(function(){
var1 = $(this).attr('class');
});
The above might, on reflection, not be quite what you're after. I'd suggest trying:
$('.bg img').click( // attaches the 'click' to the image
function(){
var1 = $(this).attr('class');
});
Or:
$(".bg").click(function(){
var1 = $(this).find('img').attr('class'); // finds the 'img' inside the 'a'
});
精彩评论