开发者

jquery multiple conditions on selector

开发者 https://www.devze.com 2023-04-13 00:29 出处:网络
I have the below jquery snippet which filters for any links which has a specific extension mentioned in the regular expression. To this condition, I want to check if the href value starts with http://

I have the below jquery snippet which filters for any links which has a specific extension mentioned in the regular expression. To this condition, I want to check if the href value starts with http://www.xyz.com . How can that be done?

$('a:regex(href,\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*))$').live('click', function (e) {
    // do some action..
});

The regex helper function is below

jQuery.expr[':'].regex = function (elem, index, match) {
    var matchParams = match[3].split(','),
        validLabels = /^(data|css):/,
        attr = {
            method: matchParams[0].match(validLabels) ?
                        matchParams[0].split(':')[0] : 'attr',
            property: matchParams.shift().replace(validLabels, '')
        },
        regexFlags = 'ig',
        regex =开发者_开发问答 new RegExp(matchParams.join('').replace(/^\s+|\s+$/g, ''), regexFlags);
    return regex.test(jQuery(elem)[attr.method](attr.property));
}


It looks to me like there's an error in the regex (with the position of the $). But here's how you would chain the selectors:

$('a[href^=http://www.xyz.com]:regex(href,\\.(zip|mp\\d+|mpe*g|pdf|docx*|pptx*|xlsx*|jpe*g|png|gif|tiff*)$)').live('click', function (e) {
    // do some action..
});
0

精彩评论

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

关注公众号