I have added a class to a list of product images eg 'productID_123', I would like to get the number from the end. I need this to be as dynamic as possible the image may have more than one class. This is what I have put together so far, I think I am nearly there just can't figure out the IF:
$(".image").each(function(){
var classList = $(this).attr('class').split(/\s+/);
$.each( classList, function(i开发者_如何学Cndex, item){
if (item === 'productID_') {
product_id = item.replace("productID_","");
fetchProductImages(product_id,this.width,this.height);
}
});
});
Maybe can I enforce that it is an integer that is returned?
I can't use the data-* attribute because the page is written in XHTML Strict and is required to validate.
You could use a regular expression:
$.each( classList, function(index, item){
var matched = item.match( /^productID_(\d+)$/ );
if (matched) {
var product_id = matched[1];
fetchProductImages(product_id,this.width,this.height);
}
});
精彩评论