简体   繁体   English

HTML Javascript-获取所有具有ID的元素

[英]HTML Javascript - Get all elements with ID

I am using the waterwheel-carousel image slider located here . 我正在使用此处waterwheel-carousel图像滑块。 I would like to have multiple carousels on one page. 我想在一页上放多个轮播。

Here is a snippet of my code: 这是我的代码片段:

<script>
    // load carousel
    $(document).ready(function () {
        $("#waterwheel-carousel").waterwheelCarousel({
            separation: 90,
            separationMultiplier: 0.2,
            horizonOffsetMultiplier: 1
        });
    });
</script>
.
.
.
<div id="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>     

<div id="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>  

<div id="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>  

And the CSS: 和CSS:

/* Projects page carousel(s) */
#waterwheel-carousel {
  width: 200px;
  height: 216px;
  position: relative;
  clear: both;
  overflow: hidden;
  margin: 0 auto;
}
#waterwheel-carousel img {
  cursor: pointer;
  border: 5px solid black;
}

The issue is that only the first #waterwheel-carousel will load. 问题是只有第一个#waterwheel-carousel会加载。 The other two do not. 其他两个没有。 I did some research and it seems like it may only be returning the first element with that id. 我做了一些研究,看来它可能只返回具有该ID的第一个元素。 So I have tried a different approach on my $(document).ready(.. function, adding class="waterwheel-carousel" to each div : 因此,我对$(document).ready(..函数$(document).ready(..尝试了一种不同的方法,向每个div添加class="waterwheel-carousel"

<script>
    $(document).ready(function () {
      var allElements = $(document).getElementsByClassName("waterwheel-carousel");
      for (var i = 0; i < allElements.length; i++) {
          var currentElement = allElements[i];
          currentElement.waterwheelCarousel({
              separation: 90,
              separationMultiplier: 0.2,
              horizonOffsetMultiplier: 1
          });
        }
    });
</script>

But then they all fail to load. 但随后它们无法加载。

Anyone have any ideas? 有人有想法么?

Thanks. 谢谢。

Element IDs should be unique within the entire document. 元素ID在整个文档中应该是唯一的。 see 看到

Instead of id use class to get all element 代替id使用class获取所有元素

<script>
    // load carousel
    $(document).ready(function () {
        $(".waterwheel-carousel").waterwheelCarousel({
            separation: 90,
            separationMultiplier: 0.2,
            horizonOffsetMultiplier: 1
        });
    });
</script>
.
.
.
<div class="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>     

<div class="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>  

<div class="waterwheel-carousel">
  <img src="" alt="one">
  <img src="" alt="two">
  <img src="" alt="three">
  <img src="" alt="four">
</div>

This is what classes are for, IDs are unique. 这就是类的用途,ID是唯一的。 Which means that each element in the document must have a different ID. 这意味着文档中的每个元素必须具有不同的ID。

Modify your HTML: 修改您的HTML:

<div class="waterwheel-carousel">
<img src="" alt="one">
<img src="" alt="two">
<img src="" alt="three">
<img src="" alt="four">
</div>     

<div class="waterwheel-carousel">
<img src="" alt="one">
<img src="" alt="two">
<img src="" alt="three">
<img src="" alt="four">
</div>  

<div class="waterwheel-carousel">
<img src="" alt="one">
<img src="" alt="two">
<img src="" alt="three">
<img src="" alt="four">
</div>

and you're good to go. 而且你很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM