$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Why won't querySelectorAll select all of my elements? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Why won't querySelectorAll select all of my elements? - Stack Overflow

matteradmin9PV0评论

In this example, the querySelectorAll selects td elements 2 through 4:

document.querySelectorAll("td + td");

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>

So, it's going to select all td elements greater than or equal to 2.

But in the following example, 'div' is not selecting both of my div elements when I use:

document.querySelectorAll('div').style.color = 'red';

<div>foo</div>
<div>bar</div>

Demo

Why doesn't querySelectorAll work in this basic scenario?

In this example, the querySelectorAll selects td elements 2 through 4:

document.querySelectorAll("td + td");

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
        <td>4</td>
    </tr>
</table>

So, it's going to select all td elements greater than or equal to 2.

But in the following example, 'div' is not selecting both of my div elements when I use:

document.querySelectorAll('div').style.color = 'red';

<div>foo</div>
<div>bar</div>

Demo

Why doesn't querySelectorAll work in this basic scenario?

Share Improve this question edited Nov 19, 2014 at 19:17 TylerH 21.1k79 gold badges79 silver badges114 bronze badges asked Jul 7, 2014 at 10:42 Navin RauniyarNavin Rauniyar 10.6k14 gold badges47 silver badges70 bronze badges 3
  • querySelectorAll – rogaldh Commented Jul 7, 2014 at 10:43
  • 2 See: developer.mozilla/en/docs/Web/API/Document.querySelectorAll. It returns a NodeList, which acts like an array. – Evan Trimboli Commented Jul 7, 2014 at 10:43
  • I already checked that but couldn't understand and now seeing the answer below I got concept. – Navin Rauniyar Commented Jul 7, 2014 at 10:45
Add a ment  | 

1 Answer 1

Reset to default 5

querySelectorAll returns an array-like object (NodeList), so you can't set the property of all matched elements like that (like jQuery). You have to iterate over them:

var divs = document.querySelectorAll('div');

for(var i=0; i<divs.length; i++){
    divs[i].style.color = "red";
}

Note: this differs from querySelector, which by contrast always returns a single element.

You can write a helper function like:

function qsa(selector, styleProperty, value){
    var divs = document.querySelectorAll('div');

    for(var i=0; i<divs.length; i++){
        divs[i].style[styleProperty] = value;
    }
    return divs;
}

Usage:

var divs = qsa('div', 'color', 'red');
Post a comment

comment list (0)

  1. No comments so far