【问题标题】:Javascript element children about function parameter关于函数参数的 Javascript 元素子项
【发布时间】:2018-06-09 19:20:08
【问题描述】:

以下是制作杂耍的部分代码。让我困惑的是: 在这种情况下,参数“容器”的等效 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";
    }

【问题讨论】:

  • 在 DOM 层次结构中直接嵌套在另一个元素中的元素是它的子元素。
  • 所以&lt;head&gt;&lt;body&gt;&lt;html&gt;的孩子,&lt;div&gt;&lt;script&gt;&lt;body&gt;的孩子,图片是&lt;div id="slideshow"&gt;的孩子

标签: javascript dom nodes element


【解决方案1】:

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

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

那么container.children就是直接嵌套在那个DIV里面的元素,都是&lt;img&gt;元素。

【讨论】:

  • 是的,我完全是个笨蛋。脚本代码最后一行是slideShow(document.getElementById("slideshow")); 难怪我这么糊涂... Thx
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-15
  • 1970-01-01
  • 2017-11-10
  • 2015-04-30
  • 2014-03-25
  • 1970-01-01
相关资源
最近更新 更多