开发者

Given the following JS/JQUERY, how to prevent it from being case sensitive

开发者 https://www.devze.com 2022-12-24 04:52 出处:网络
I have the following line of code which works well: $(\"a.grouped_elements[href$=.jpg],a.grouped_elements[href$=.png],a.grouped_elements[href$=.gif]\").fancybox();

I have the following line of code which works well:

$("a.grouped_elements[href$=.jpg],a.grouped_elements[href$=.png],a.grouped_elements[href$=.gif]").fancybox();

Problem is, if the href is .JPG it doesn't work it only works with .jpg. How can I make the above case insensitive so either a开发者_JAVA技巧ll caps or no caps or a mix all match for the file extension?

Thanks


Apparently, "a filter function should do what you need".

Since I am not familiar with filter functions, I would try something like the following, though I am not sure how efficient/inefficient it might be compared to filter functions mentioned above:

$('a.grouped_elements').each(function(){
    var elem = $(this);
    if(elem.attr('href').match(/(gif|jpg|png)$/i) != null) {
        elem.fancybox();
    }
});

(hasn't been tested but that's the general gist of it)


Using filter as also mentioned by @Ricket. The idea is to return true if we want the element to be included, false otherwise.

var fancyBoxable = $('a.groupd_elements').filter(function(){
    var href = $(this).attr('href') || '';
    return /(gif|jpg|png)$/i.test(href);
});

fancyBoxable.fancybox()


Ok this ended up working:

$('a.grouped_elements').each(function(){
    var elem = $(this);
    // Convert everything to lower case to match smart
    if(elem.attr('href').toLowerCase().match('/gif|jpg|png/') != null) {
        elem.fancybox();
    }
});
0

精彩评论

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