Javascript, CSS: Get element by style attribute

后端 未结 5 1686
逝去的感伤
逝去的感伤 2020-12-03 17:38

I\'d like to:

  1. Find a style attribute for all elements in the page (for instance: all elements that have color:#333;)
  2. Change this attribut
5条回答
  •  长情又很酷
    2020-12-03 18:38

    It's as already said really hard / inefficient to query all elements by color.

    // refrence: http://stackoverflow.com/questions/5999209/jquery-how-to-get-the-background-color-code-of-an-element
    var arr = [];
    
    $('*').each(function (i, ele) {
       // is red => save
       if($(ele).css('backgroundColor') == ('rgb(0, 0, 255)')) arr.push(ele);
    });
    
    console.log(arr);
    

    Here is an JSFiddle Example for it: http://jsfiddle.net/ddAg7/

    My recommendation for this is: Don't do it!

提交回复
热议问题