jQuery 通过内联 css 属性查找

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1180067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:50:42  来源:igfitidea点击:

jQuery find by inline css attribute

jquerycss

提问by Sammy

I need to find an element based on a specific cssattribute. The cssis applied inline and I can not use a class. Is there anyway to achieve this in jQuery?

我需要根据特定css属性找到一个元素。在css被内联应用,我不能使用的类。有没有办法做到这一点jQuery

回答by Russ Cam

You could use an attributeselector

您可以使用属性选择器

For example

例如

$(":radio [style*='regular']")

would return a wrapped set of any input radios that contain 'regular'in the style attribute

将返回包含'regular'在 style 属性中的任何输入无线电的包装集

回答by TMS

I think this is the cleanest solution. Say you want to find all elements where z-index has particular value:

我认为这是最干净的解决方案。假设您要查找 z-index 具有特定值的所有元素:

$('*').filter(function () { return $(this).css('z-index') == 101 })

回答by Surya

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

我认为您可以编写一个自定义jQuery选择器来执行此操作。

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')')

回答by karim79

Something like:

就像是:

$('selector').each(function() {
    if($(this).attr('style').indexOf('font-weight') > -1) {
        alert('got my attribute');
    }
});

回答by sBo

Yes, I think the fastest and easiest way to achieve the desired results would be to target styles with a property class selector.

是的,我认为实现预期结果的最快和最简单的方法是使用属性类选择器定位样式。

Like stated before:

就像之前说的:

$('div.yourClass[style*="background-color:Blue"]).css('color', 'White');

That way we target all divs with same class that have blue background and tell them all to have white color text. Of course instead of .csswe can use anything here, .animateand tell all blues to be animated etc. This helped me a lot with DB relations and people setting up strange things in DB.

这样,我们将所有具有蓝色背景的具有相同类的 div 作为目标,并告诉它们都具有白色文本。当然,.css我们可以在这里使用任何东西,.animate并告诉所有蓝调动画等等。这对我与 DB 关系和人们在 DB 中设置奇怪的东西有很大帮助。

I used to be less generic, and target strings with :contains... but then somebody goes to the base, and changes texts and labels, and bye bye code ;)

我以前不太通用,目标字符串带有 :contains... 但后来有人去基地,改变文本和标签,再见代码;)

Hope it helps.

希望能帮助到你。

回答by mkoryak

var ccsStyle="font-weight:bold";
var els = [];
$("*").each(function(){
  var st = $(this).attr("style");
  if(st.indexOf(cssStyle) > -1) els.push(this);
});
//do stuff with els

回答by MacAnthony

You could use the attribute flag and use contains if you know the specific format that it's in.

如果您知道它所采用的特定格式,则可以使用属性标志并使用 contains。

$("[style*='position']")

This would find all the elements that define the position css attribute.

这将找到所有定义位置 css 属性的元素。

回答by Mihnea Belcin

You can search for any inline attribute with .attr().. .attr('attr_name')

您可以使用.attr()..搜索任何内联属性。.attr('attr_name')

http://api.jquery.com/attr/

http://api.jquery.com/attr/

css_inline_value = $(element).attr('style');

if (css_inline_value = search_value) {
    // do stuff
}