首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >DOM和events,找到正确的元素

DOM和events,找到正确的元素
EN

Stack Overflow用户
提问于 2022-05-03 05:55:36
回答 1查看 21关注 0票数 0

我必须编写一个函数,它以博客id作为参数(例如'blog-1''blog-2'),然后,从具有与参数匹配的id的元素中,函数需要选择具有属性class="content"的所有元素。最后,函数需要返回从前一步获得的元素。

HTML:

代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
    <meta content="utf-8" http-equiv="encoding">
  </head>
  <body>
    <h2>Our Amazing Blogs</h2>
    <div id="blog-1">
      <h3>How to prepare the perfect pizza</h3>
      <p class="content">
        The secret for preparing the perfect pizza is.... #!/*?@, and ####@@@!</br>
        So you just have to $%!?#**- it!
      </p>
      <p class="content">
        <u>Upgrade to Pro to uncover the full post</u>
      </p>
    </div>
    <div id="blog-2">
      <h3>My life as a shrimp</h3>
      <p class="content">
        Few years back I got tired of all the pressure put on me just for the fact of being a human being.
        Why does society expect you to behave like a person just for the fact of being one?!
        So I decided to turn myself into a shrimp and what happend next is amazing...
      </p>
      <p class="content">
        <u>Upgrade to Pro to uncover the full post</u>
      </p>
    </div>
  </body>
</html>

Javascript基础:

代码语言:javascript
运行
复制
function getContentFromBlog(blogId) {
  // Get the right div according to the given blogid
  let blog = "Replace this string with the expression to get the blog div";
  // Get all elements with content class from the blog div
  let content = "Replace this string with the expression to get the elements with 'content' class from the blog div";
  // Write the return statement to return content
}
EN

回答 1

Stack Overflow用户

发布于 2022-05-03 07:31:22

代码语言:javascript
运行
复制
function getContentFromBlog(blogid) {
  const div = document.querySelectorAll(`div#${blogid} p.content`);
  if (!div) {
    return;
  }
  return Array.from(div).map((ele) => ele.textContent.split("\n").join(''));
}
console.log(getContentFromBlog("blog-1"));
代码语言:javascript
运行
复制
<!DOCTYPE html>
<html lang="en">

<head>

  <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
  <meta content="utf-8" http-equiv="encoding">
</head>

<body>

  <h2>Our Amazing Blogs</h2>

  <div id="blog-1">
  
    <h3>How to prepare the perfect pizza</h3>
  
    <p class="content">
      The secret for preparing the perfect pizza is.... #!/*?@, and ####@@@!</br>
      So you just have to $%!?#**- it!
    </p>
  
    <p class="content">
      <u>Upgrade to Pro to uncover the full post</u>
    </p>
    </div>

  <div id="blog-2">
  
    <h3>My life as a shrimp</h3>
  
    <p class="content">
      Few years back I got tired of all the pressure put on me just for the fact of being a human being. Why does society expect you to behave like a person just for the fact of being one?! So I decided to turn myself into a shrimp and what happend next is
      amazing...
    </p>
  
    <p class="content">
      <u>Upgrade to Pro to uncover the full post</u>
    </p>
    </div>
  </body>

</html>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72095296

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档