jquery每个regexp href同时检查一些字符串

时间:2021-09-19 11:47:39

people!

人!

I have this:

我有这个:

$('.download_video a[href*="flv"]').each(function() {

        $( this ).addClass('videoCl');

});

I would like to perform many checks in a single code line like:

我想在一个代码行中执行许多检查,如:

$('.download_video a[href*="flv|mpeg|ogg"]').each(function() {

        $( this ).addClass('videoCl');

});

I've realized flv|mpeg|ogg is not working. I think it is a matter of little detail...

我已经意识到flv | mpeg | ogg无法正常工作。我认为这是一个细节问题......

Anyone could help me?

有人可以帮帮我吗?

Tks a lot!

Tks很多!

2 个解决方案

#1


3  

You can't put multiple attribute selectors in a single selector. However, you can combine multiple selectors using comma:

您不能在单个选择器中放置多个属性选择器。但是,您可以使用逗号组合多个选择器:

$('.download_video')
    .find('a[href*=flv], a[href*=mpeg] a[href*=ogg]')
    .addClass('videoCl');

Notice that you don't need to use .each if you're doing the same operation to each element; jQuery automatically maps over the entire set.

请注意,如果对每个元素执行相同的操作,则不需要使用.each; jQuery自动映射整个集合。

#2


1  

Try

尝试

    $.fn.attrs = function () {
        var args = arguments[0];
        var elem = $(this);
        var map = $.map(arguments[1], function (v, k) {
            return ($("[" + args + "*=" + v + "]").is(elem) 
                    ? elem[0].nodeName.toLowerCase() 
                      + "[" + args + "*=" + v + "]" 
                    : null);
        });
        return $(map.toString());
    };

$(".download_video a")
.attrs("href", ["mpeg", "flv", "ogg"]).addClass("videoCl");

jsfiddle http://jsfiddle.net/guest271314/834q1awc/

jsfiddle http://jsfiddle.net/guest271314/834q1awc/

#1


3  

You can't put multiple attribute selectors in a single selector. However, you can combine multiple selectors using comma:

您不能在单个选择器中放置多个属性选择器。但是,您可以使用逗号组合多个选择器:

$('.download_video')
    .find('a[href*=flv], a[href*=mpeg] a[href*=ogg]')
    .addClass('videoCl');

Notice that you don't need to use .each if you're doing the same operation to each element; jQuery automatically maps over the entire set.

请注意,如果对每个元素执行相同的操作,则不需要使用.each; jQuery自动映射整个集合。

#2


1  

Try

尝试

    $.fn.attrs = function () {
        var args = arguments[0];
        var elem = $(this);
        var map = $.map(arguments[1], function (v, k) {
            return ($("[" + args + "*=" + v + "]").is(elem) 
                    ? elem[0].nodeName.toLowerCase() 
                      + "[" + args + "*=" + v + "]" 
                    : null);
        });
        return $(map.toString());
    };

$(".download_video a")
.attrs("href", ["mpeg", "flv", "ogg"]).addClass("videoCl");

jsfiddle http://jsfiddle.net/guest271314/834q1awc/

jsfiddle http://jsfiddle.net/guest271314/834q1awc/