Javascript 如何从包含特定类的页面中删除所有元素
我想打印我的LinkedIn个人资料作为我的简历。我通过Chrome控制台使用单独的JQuery行删除了一些不需要的元素,例如以下示例: $'.header'。删除; $'.topcard_uu连接'。删除; $“.li页脚”。删除; 该网页有一个项目部分,其中有许多空白肖像。我想删除所有的肖像元素。我可以单独这样做,例如以下示例: document.querySelectorbody>main>div.core-rail>section> section.projects.pp-section>ul>li:nth-child27>ul.remove; document.querySelectorbody>main>div.core-rail>section> section.projects.pp-section>ul>li:nth-child28>ul.remove; 有20多个这样的元素。如何以编程方式删除满足以下条件的所有元素Javascript 如何从包含特定类的页面中删除所有元素,javascript,jquery,html,google-chrome,element,Javascript,Jquery,Html,Google Chrome,Element,我想打印我的LinkedIn个人资料作为我的简历。我通过Chrome控制台使用单独的JQuery行删除了一些不需要的元素,例如以下示例: $'.header'。删除; $'.topcard_uu连接'。删除; $“.li页脚”。删除; 该网页有一个项目部分,其中有许多空白肖像。我想删除所有的肖像元素。我可以单独这样做,例如以下示例: document.querySelectorbody>main>div.core-rail>section> section.projects.pp-section
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">
我希望这样可以去除每个元素中的肖像。相反,它给出了未定义的输出。在调用这些函数之前,确保已加载项目部分。 因为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() );
}
与其浏览和删除内容,为什么不选择您真正想要打印的内容?请澄清问题。标题在你身体的第一小段得到了回答,身体非常模糊。据我所知,您只需要使用包含多个类的选择器来删除元素。为此,只需执行$face-pile\uuuuu list.projects-face-pile\uuuu list.remove;您好@EternalHour,谢谢您的评论。我有两个理由。首先,我要打印的元素比我要删除的元素多得多,所以删除它们似乎更快。其次,我只想为特定应用程序包含许多元素。我想要一个脚本,删除所有不属于任何应用程序的内容,然后我可以手动删除不适合该特定应用程序的少数元素。嗨@JesseGibson,谢谢你的建议。我编辑了尸体。我尝试运行您的代码并收到以下错误:VM984:1 Uncaught TypeError:无法在1:46读取null的属性'remove'。我忘记了句点。尝试$.face-pile\u list.projects-face-pile\u list.remove;你好,伊利亚·沙班,谢谢你的回答。我相信项目部分已经加载,因为我可以在网页上看到肖像,当我打开Chrome控制台时,我也可以在元素选项卡上看到它们。当我运行这些函数时,我得到的结果是未定义的,而当我运行removeCreators时,我得到的错误是:Uncaught TypeError:无法读取removeCreators:3:49 at:1:1处null的属性'querySelectorAll'?
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() );
}