开发者

JQuery Get last from multiple class values

I have a couple of div's that look like this:

<div class="film cars-0317219"></div>
<div class="film wall-e-0910970"></div>
<div class="film finding-nemo-0266543"></div>
<div class="film cars-0317219"></div>

Im concerned with the second (so last) class name, but only of the divs that have the class 'film'. Is there any way I can get the last class name with JQuery?

To give you an example of what开发者_开发知识库 I want to do look below. I want to make these div's clickable and let the visitor go to /title/last-class-name.html

$('div.film').live('click', function(){

    // This returns the whole 'film random-title-id'
    // instead of just 'random-title-id'
    var id = $(this).attr('class');

    location.href = '/title/' + id + '.html';

});


This should work:

$('div.film').live('click', function(){

    var classes=$(this).attr("class").split(" ");
    var id=classes[classes.length-1];

    location.href = '/title/' + id + '.html';

});

although if you don't already have IDs on your elements, this seems like it would make more sense as an ID.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜