【发布时间】: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