Retrieve the last class in an element that has mutiple classes
How can I retrieve the last class in an element that has multiple classes assigned to it using Jquery ie.
<div class="class1 class2 class3"><开发者_运维知识库;/div>
var cls = $('div').attr('class').split(' ');
alert(cls[cls.length-1]);
demo
Here's a regular expression version if you prefer.
var lastClass = $(selector).attr('class').match(/\s([^\s]+)$/)[1];
You want to retrieve class3
in this case, am I right?
var lastClassName = $.trim($("elementRef").attr("class")).split(" ").slice(-1);
精彩评论