The browser pop-up is always different from the one in the book. I don’t understand. Logically speaking, it should be 1. Why did it show 0 when I tried it?
code show as below:
<!DOCTYPE html>
<html>
<body>
<p class="example">123</p>
<p class="example">123</p>
<p class="example">123</p>
<p>456</p>
<p id="demo"></p>
<script>
alert(document.getElementsByClassName("example").length);
</script>
</body>
</html>
曾经蜡笔没有小新2017-06-26 10:57:34
The pop-up is 3, because there are three elements with ClassName as example.
仅有的幸福2017-06-26 10:57:34
Returns an array-like object containing all child elements of the specified class name. When the call occurs on a document object, the entire DOM is searched, including the root node. You can also call the getElementsByClassName() method on any element. It will return all child elements with the specified class name with the current element as the root node.
You use getElementsByClassName()
to select all nodes with class names example
, and popup 3 is correct
PHP中文网2017-06-26 10:57:34
The pop-up answer is 3, because there are three classes named example.