jQuery selector to target any class name (of multiple present) starting with a prefix?

前端 未结 4 1379
栀梦
栀梦 2020-12-02 19:30

I\'m considering one selection statement that would target one of many css class names in a single class attribute value based on a string prefix.

For example, I wan

4条回答
  •  悲哀的现实
    2020-12-02 20:10

    You could also write a small plugin that uses .map() to walk through the classes.

    $.fn.classBeginsWith = function(str){
      return !str ? this : this.filter( function(){
        return $.map(this.className.split(' '), function(e,i){
          if (e.match(new RegExp('^' + str, 'ig'))) return 1;
        }).length;
      });
    }
    

    And then:

    $('a').classBeginsWith('detail-')
    

提交回复
热议问题