简体   繁体   English

我不明白的任务

[英]A task I don't understand

Hi i have one task and I can't solve,the excercise is find div with class = "article" using classList property,else return what that post is not found here is a code of divs嗨,我有一个任务,我无法解决,练习是使用 classList 属性找到带有 class = "article" 的 div,否则返回这里找不到的帖子是 div 的代码

 <div class="content" class="atricle">
      <p>
        Lorem ipsum,<a href="#" class="link">link</a> dolor sit amet consectetur
        adipisicing elit. Quas quidem doloribus dolor, qui doloremque deleniti
        alias. Iure, praesentium at? Esse non assumenda enim id in molestiae
        earum neque hic numquam!
      </p>
    </div>
    <div class="content" class="news_post">
      <p>
        Lorem ipsum,<a href="#" class="link">link</a> dolor sit amet consectetur
        adipisicing elit. Quas quidem doloribus dolor, qui doloremque deleniti
        alias. Iure, praesentium at? Esse non assumenda enim id in molestiae
        earum neque hic numquam!
      </p>
    </div>

here is my solve这是我的解决方案

const allPosts = document.querySelectorAll("div")
function searchRes(allPosts){
if(allPosts.classList.toggle("article") = true){
  console.log(allPosts.article)
}else{
  console.log("There is no articles")
}
}
searchRes()

help me please:(请帮帮我:(

You will need to determine whether you are interested in exact or loose matches, that is, a div with a class being exactly equal to article is an exact match, while a div whose class is containing article is a loose match.您需要确定您对完全匹配还是松散匹配感兴趣,即class完全等于articlediv是精确匹配,而class包含articlediv是松散匹配。 This is why c is part of the second result, but not of the first, whereas p cannot be a result, because, even though it has a matching class, it's not a div .这就是为什么c是第二个结果的一部分,而不是第一个结果的一部分,而p不能是结果,因为即使它有一个匹配的类,它也不是div Note that the articles class is correctly ignored.请注意, articles class被正确地忽略了。

 let exactMatches = document.querySelectorAll("div[class=article]"); if (exactMatches.length) { let exactValues = [...exactMatches].map(item => item.innerHTML); console.log("Exact matches are: " + JSON.stringify(exactValues)); } else { console.log("No matches were found"); } let looseMatches = [...document.querySelectorAll("div[class*=article]")].filter(item => item.classList.contains("article")); if (looseMatches.length) { let looseValues = looseMatches.map(item => item.innerHTML); console.log("Loose matches are: " + JSON.stringify(looseValues)); } else { console.log("No matches were found"); }
 <div class="article">a</div> <div class="article">b</div> <div class="article something">c</div> <p class="article">d</div> <div class="articles something">e</div>

The [...somevariable] converts somevariable into an array, in our case, the result of querySelectorAll is an arraylike object (see more here https://www.capscode.in/blog/what-is-array-like-object-in-javascript ) and as such, it des not have a filter method, so I converted it into an array in order to be able to neatly filter it. [...somevariable]somevariable转换为数组,在我们的例子中, querySelectorAll的结果是一个类似数组的对象(更多信息请参见此处https://www.capscode.in/blog/what-is-array-like-object -in-javascript ) 因此,它没有filter方法,所以我将它转换成一个数组以便能够整齐地过滤它。

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

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