【问题标题】:How remove all elements from a page which contain specific class如何从包含特定类的页面中删除所有元素
【发布时间】:2023-03-25 01:00:01
【问题描述】:

我想打印我的 LinkedIn 个人资料 (https://www.linkedin.com/in/peterhorniak/) 作为我的简历。我已经通过 Chrome 控制台使用单独的 JQuery 行删除了一些不需要的元素,例如以下示例:

  • $('.header').remove();
  • $('.topcard__connections').remove();
  • $('.li-footer').remove();

该网页有一个项目部分,其中包含许多空白肖像。我想删除所有的肖像元素。我可以单独这样做,例如这些示例:

  • document.querySelector("body > main > div.core-rail > section > section.projects.pp-section > ul > li:nth-child(27) > ul").remove();
  • document.querySelector("body > main > div.core-rail > section > section.projects.pp-section > ul > li:nth-child(28) > ul").remove();

这样的元素有 20 多个。如何以编程方式删除满足以下条件的所有元素?

 1. they are part of the section <section class="projects pp-section" data-section="projects">
 2. they are part of an unordered list <ul class="projects__list">
 3. they are one of the list elements <li class="projects__project">
 4. within that list element, they are in the unordered list <ul class="face-pile__list projects-face-pile__list">

我正在运行 Chrome 版本 76.0.3809.100(官方版本)(64 位)。

我尝试过基于this question 进行循环。因为类里面有空格,并且基于this question,所以我把它当作两个类来处理。

var projects = document.getElementsByClassName("face-pile__list", "projects-face-pile__list");
while (projects.length)
    projects[0].classList.remove("face-pile__list", "projects-face-pile__list");

我希望这会从每个元素中删除肖像。相反,它给出了“未定义”的输出。

【问题讨论】:

  • 与其经历并删除东西,为什么不直接选择您真正想要打印的内容?
  • 请澄清问题。标题在你身体的第一个点点回答,身体很模糊。据我所知,您只想使用包含多个类的选择器来删除元素。为此,您可以简单地执行$("face-pile__list.projects-face-pile__list").remove();
  • 嗨@EternalHour,感谢您的评论。我有两个原因。首先,我要打印的元素比要删除的元素多得多,因此删除它们似乎更快。其次,我只想为特定应用程序包含许多元素。我想要一个脚本来删除不属于任何应用程序的所有内容,然后我可以手动删除一些不适合该特定应用程序的元素。
  • 嗨@JesseGibson,感谢您的建议。我已经编辑了正文。我尝试运行您的代码并收到此错误:VM984:1 Uncaught TypeError: Cannot read property 'remove' of null at :1:46
  • 我忘记了一个时期。试试$(".face-pile__list.projects-face-pile__list").remove();

标签: javascript jquery html google-chrome element


【解决方案1】:

确保在调用这些函数之前加载了项目部分。 由于 Linkedin 使用延迟加载,除非您打开项目部分 手动不会在页面上加载任何信息

function removeCreators() {
    const projectSection = document.querySelector('.pv-profile-section.pv-accomplishments-block.projects');
    const contributorsSections = projectSection.querySelectorAll('a[data-control-name="project_contributors"]');
    contributorsSections.forEach( x => x.remove() );
};

function removeEmptyCreatorsImages() {
  const projectSection = document.querySelector('.pv-profile-section.pv-accomplishments-block.projects');
  const emptyContributors = projectSection.querySelectorAll('img.ghost-person');
  emptyContributors.forEach( x => x.remove() );
}

【讨论】:

  • 嗨@Illia Chaban,感谢您的回答。我相信项目部分已加载,因为我可以在网页上看到肖像,当我打开 Chrome 控制台时,我也可以在 Elements 选项卡中看到它们。当我运行这些函数时,我得到结果“未定义”,然后当我运行“removeCreators()”时,我得到错误:Uncaught TypeError: Cannot read property 'querySelectorAll' of null at removeCreators (:3:49) at :1:1 请问我做得对吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-20
  • 2014-11-08
  • 2016-08-16
相关资源
最近更新 更多