Getting class name of last child element
I want to retrieve the class name of the last child element in .find_class
, but my code gives me undefined
. How can I fix it?
Example: http://jsfiddle.net/gBxan/
<div class="find_class">
<div clas开发者_开发百科s="class1"></div>
<div class="class2"></div>
<div class="class3"></div> <!-- I want to get this div's class name -->
</div>
var find = $('div.find_class div:last').find('div:last').attr('class');
alert(find);
You need to lose the extra find
:
var cls = $('div.find_class div:last').attr('class');
See it in action.
maybe the following code will do the job:
var divs = $(".find_class").find("div");
var lastDiv = divs.eq(divs.length-1);
alert(lastDiv.attr("class"));
Your example in the question is slightly different than your example in the replies. You only want to be looking at the immediate child divs.
Try this:
var find = $('div.find_class > div:last').attr('class');
alert(find);
See http://jsfiddle.net/VbxpY/
精彩评论