[英]How access second div with same id using querySelectorAll javascript?
Here, I have two div on my webpage both div have almost same data. 在这里,我的网页上有两个div,两个div的数据几乎相同。 accept type attribute.
接受类型属性。
My first div attribute have type="timeline". 我的第一个div属性具有type =“ timeline”。 and Second div attribute type ="slideshow".
和第二个div属性类型=“ slideshow”。
When my page load it only detects the first div. 当我的页面加载时,它仅检测到第一个div。
But I have a condition if my div type equal to slideshow then my code run but it detects only first div. 但是我有一个条件,如果我的div类型等于幻灯演示,那么我的代码就会运行,但它只会检测到第一个div。
Here is my code. 这是我的代码。
<div type='timeline' id='slide'> <section> <header>Title One</header> <article>Content</article> </section> <section> <header>Title Two</header> <article>Content</article> </section> <section> <header>Title Three</header> <article>Content</article> </section> <section> <header>Title Four</header> <article>Content</article> </section> </div>
var divSlide = document.querySelectorAll('#slide'); var myNodeList = divSlide.length; for(i = 0; i < myNodeList.length; i++) { var divMain = document.getElementById("slide"); console.log(divMain); var align = divMain.getAttribute("type"); console.log(align); console.log(myNodeList) if(align=='slideshow'){ console.log("my working code"); } }
What should i do. 我该怎么办。 Help will be appreciated.
帮助将不胜感激。
You have a few of mistakes in your code that we need to fix first. 您的代码中有一些错误,我们需要首先修复。
First, you shouldn't use the same id value more than once in your code so you need to replace the id with a class (even though our code can still work with id here). 首先,您不应在代码中多次使用相同的id值,因此您需要用一个类替换id(即使我们的代码在此处仍可以使用id)。
Second mistake - in your for loop you are using the wrong variable "myNodeList.length", myNodeList variable already is the length so it does not have a length property. 第二个错误 -在for循环中,您使用了错误的变量“ myNodeList.length”,myNodeList变量已经是长度,因此它没有length属性。 you should instead use the divSlide variable like this:
您应该改为使用divSlide变量,如下所示:
for(i = 0; i < divSlide.length; i++)
Third mistake- in order to access the current item that is being looped over you should use the variable that holds the list (which is divSlide here)and then add to it i in brackets to indicate the current index in use, like this 第三个错误 -为了访问正在循环的当前项目,您应该使用保存列表的变量(此处为divSlide ),然后将其添加到方括号i中以指示当前正在使用的索引,例如
var divMain = divSlide[i];
// instead of this: var divMain = document.getElementById("slide");
Fourth - you should in most cases use triple = signs to check for values. 第四 -在大多数情况下,您应该使用Triple =符号来检查值。 so instead of == you should use ===
因此,应该使用===而不是==
this is how the code will look like finally: 这是代码的最终外观:
var divSlide = document.querySelectorAll('#slide');
var myNodeList = divSlide.length;
document.addEventListener("DOMContentLoaded", functionName)
function functionName(){
for(i = 0; i < divSlide.length; i++) {
var divMain = divSlide[i];
var align = divMain.getAttribute("type");
if(align ==='slideshow'){
console.log("my working code");
}
}
}
You can get div
by type
attribute like this: 您可以像这样通过
type
属性获取div
:
HTML HTML
<div type='timeline' id='slide'>
this is timeline
</div>
<div type='slideshow' id='slide'>
this is slideshow
</div>
JS JS
$('#slide[type=slideshow]') // get div that has id='slide' and type='slideshow'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.