关于函数参数的Javascript元素子元素

时间:2017-12-29 10:25:10

标签: javascript dom nodes element

以下是制作旁观的代码的一部分。令我困惑的是: 在这种情况下,参数“container”的等效DOM节点是什么? 有很多元素,它如何认为<img>元素是container.children?

<!DOCTYPE html>
<html>
    <head>
        <title></title>
    </head>
    <body>

    <div id="slideshow">
        <img src="https://www.kasandbox.org/programming-images/animals/birds_rainbow-lorakeets.png" alt="Rainbow lorakeets" />
        <img src="https://www.kasandbox.org/programming-images/animals/butterfly.png"alt="Butterfly" />
        <img src="https://www.kasandbox.org/programming-images/animals/cat.png" alt="Cat" />
        <img src="https://www.kasandbox.org/programming-images/animals/crocodiles.png" alt="Crocodiles" />
        <img src="https://www.kasandbox.org/programming-images/animals/fox.png" alt="Fox" />

    </div>

    <script>
var slideShow = function(container) {
    this.images = [];
    this.curImage = 0;
    for (i = 0; i < container.childElementCount; i++) {
        this.images.push(container.children[i]);
        this.images[i].style.display = "none";
    }

1 个答案:

答案 0 :(得分:0)

container是您拨打new slideShow()时传递的任何元素。在你的情况下,它应该是:

var ss = new slideShow(document.getElementById('slideshow'));

然后container.children是直接嵌套在该DIV中的元素,它们都是<img>个元素。