简体   繁体   English

如何使用querySelectorAll javascript访问具有相同ID的第二个div?

[英]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");
    }
 } 

}

and this is codepen example 这是codepen的例子

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'

Online Demo (jsFiddle) 在线演示(jsFiddle)

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

相关问题 如何使用querySelectorAll获取具有特定id的div中的所有div - How to get all the divs within a div with certain id using querySelectorAll 如何使用queryselectorAll方法获取多个div id - How to get multiple div id using queryselectorAll method 如何在不使用** jquery **,** querySelectorAll()**方法或** querySelector()**方法的情况下通过javascript获得与数组相同的ID(幻灯片)? - How can I get the same id (slides) as array through javascript without using **jquery**, **querySelectorAll()** Method or **querySelector()** Method? 如何使用jQuery访问同一“表TD”中的div ID - how to access div id in same “table td” using jquery 如何使用.querySelectorAll 使用 javascript 创建打字机效果? - How To Create a Typewriter effect with javascript using .querySelectorAll? 使用JavaScript隐藏多个具有相同ID的DIV中的单个DIV - Hide a single DIV out of multiple DIV with same id using javascript JavaScript:如何使用第一个表的节点访问div中的第二个表 - JavaScript: How to access the second table inside a div using the node of the first table javascript querySelectorAll()-如何 - javascript querySelectorAll() - how to 如何在javascript中使用ID将文本附加到div? - How to append text to div using ID in javascript? 如何使用JavaScript通过ID隐藏HTML div - how to hide an html div by id using javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM