HTML Javascript-获取带有ID的所有元素
HTML Javascript - Get all elements with ID
我使用的是位于此处的waterwheel-carousel
图像滑块。我想在一页上有多个转盘。
以下是我的代码片段:
<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>
和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;
}
问题是只有第一个#waterwheel-carousel
将被加载。另外两个没有。我做了一些研究,看起来它可能只返回带有该id的第一个元素。所以我在$(document).ready(..
函数上尝试了一种不同的方法,将class="waterwheel-carousel"
添加到每个div
:
<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>
但随后它们全部都无法加载。
有人有什么想法吗?
谢谢。
元素ID在整个文档中应该是唯一的。参见
使用类来获取所有元素,而不是id
<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>
这就是类的作用,ID是唯一的。这意味着文档中的每个元素都必须有不同的ID。
修改您的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>
你可以走了。
相关文章:
- 选择不带选择器的元素,仅使用元素ID
- 从元素id中获取值,而不是从javascript中的名称中获取值
- 如何将第二个元素id注入到网站元素中
- 将 JavaScript 元素 ID 传递给 PHP 或 Form Variable
- 如何在 javascript 中将表单中的元素 id 值作为数组传递
- 用于指定元素 ID 的正确 JavaScript 语法是什么
- 如何从 jQuery 中的 ajax 函数中的类访问元素 ID
- JavaScript获取多个已知元素ID
- 使用ng repeat时元素ID重复
- 在使用onblur进行输入评估时,是否可以获得单击对象的元素id
- JQuery通过变量标识元素id:多个表行
- 当元素ID包含美元符号时,Javascript中的getElementByid.当ID包含美元符号时,
- 我得到未捕获的类型错误:无法读取属性'选项'即使定义了元素id,也为null
- 元素id未被接受为函数参数
- 使用jquery获取类元素id
- 如何在没有任何元素id的情况下检索表中的文本内容
- jquery按类确定元素id
- 获取元素ID并传递到函数中
- 使用jquery获取元素id
- JavaScript文档.getElementById(“id”)和元素id属性