How do I select elements on multiple attribute values

前端 未结 4 1779
孤城傲影
孤城傲影 2020-12-01 13:32

With jQuery, it is easy to select elements with a given attribute value.

For example:

var elements = $(\'div[attr1=\"value1\"]\');

相关标签:
4条回答
  • 2020-12-01 13:59

    Since jquery uses CSS selectors, as defined by the CSS specification a selector with multiple conditions will look like:

    $('div[attr1="value1"][attr2="value2"]')
    

    see the CSS spec for further reference: http://www.w3.org/TR/CSS2/selector.html#matching-attrs

    0 讨论(0)
  • 2020-12-01 14:01

    You could for example chain and filter like so

    var elements = $('div[attr1="value1"]').filter('div[attr2="value2"]');
    
    0 讨论(0)
  • 2020-12-01 14:02

    Find this solution quite simple.

    $('[attr1="home"][attr2="settings"]')
    
    0 讨论(0)
  • 2020-12-01 14:08

    To select multiple attributes, see the code below.

    This code will find all inputs that have an id attribute and whose name attribute ends with 'man' and sets the value.

    $( "input[id][name$='man']" ).val( "this input has id and name ends with 'man'" );
    
    0 讨论(0)
提交回复
热议问题