jQuery find by inline css attribute

后端 未结 8 1886
深忆病人
深忆病人 2020-12-06 00:14

I need to find an element based on a specific css attribute. The css is applied inline and I can not use a class. Is there anyway to achieve this i

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-06 00:35

    I think you might be able to write a custom jQuery selector to do this.

    For example, if you want to do select by certain style attribute, you can do:

     jQuery.extend(jQuery.expr[':'], {
        styleEquals: function(a, i, m){
            var styles = $(a).attr("style").split(" ")
            var found = false;
            for (var i = 0; i < styles.length; i++) {
                if (styles[i]===m[3]) {
                    found = true;
                    break;
                }
            }
            return found;
        }
    });
    

    Then it can be used in conjuction with any other selector you can select all input elements with certain style like this:

    $('input:styleEquals('width=10px')')
    

提交回复
热议问题