How to use querySelectorAll only for elements that have a specific attribute set?

后端 未结 3 418
深忆病人
深忆病人 2020-12-22 20:57

I\'m trying to use document.querySelectorAll for all checkboxes that have the value attribute set.

There are other checkboxes on the page t

3条回答
  •  臣服心动
    2020-12-22 21:33

    With your example:

    
    

    Replace $$ with document.querySelectorAll in the examples:

    $$('input') //Every input
    $$('[id]') //Every element with id
    $$('[id="c2"]') //Every element with id="c2"
    $$('input,[id]') //Every input + every element with id
    $$('input[id]') //Every input including id
    $$('input[id="c2"]') //Every input including id="c2"
    $$('input#c2') //Every input including id="c2" (same as above)
    $$('input#c2[value="DE039230952"]') //Every input including id="c2" and value="DE039230952"
    $$('input#c2[value^="DE039"]') //Every input including id="c2" and value has content starting with DE039
    $$('input#c2[value$="0952"]') //Every input including id="c2" and value has content ending with 0952
    $$('input#c2[value*="39230"]') //Every input including id="c2" and value has content including 39230
    

    Use the examples directly with:

    const $$ = document.querySelectorAll.bind(document);
    

    Some additions:

    $$(.) //The same as $([class])
    $$(div > input) //div is parent tag to input
    document.querySelector() //equals to $$()[0] or $()
    

提交回复
热议问题