javascript - 如何从具有多个兄弟姐妹的div中删除文本
问题描述
<div class="latest_posts style3 latest_posts--style2 latest_posts2 clearfix eluidf4c60403 latestposts2--dark element-scheme--dark">
<h3 class="m_title m_title_ext text-custom latest_posts2-elm-title" itemprop="headline"></h3>
VIEW ALL
<ul class="posts latest_posts2-posts">
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/law-for-bank/" itemprop="headline">Law For Bank</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/tech-blog/" itemprop="headline">Tech Blog</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/blog-about-tech-and-telcos/" itemprop="headline">Blog About Tech and Telcos</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
</ul>
<div>
<div class="latest_posts style3 latest_posts--style2 latest_posts2 clearfix eluidf4c60403 latestposts2--dark element-scheme--dark">
<h3 class="m_title m_title_ext text-custom latest_posts2-elm-title" itemprop="headline"></h3>
VIEW ALL
<ul class="posts latest_posts2-posts">
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/law-for-bank/" itemprop="headline">Law For Bank</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/tech-blog/" itemprop="headline">Tech Blog</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/blog-about-tech-and-telcos/" itemprop="headline">Blog About Tech and Telcos</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
</ul>
<div>
`
查看银行的所有法律 2020 年 12 月 4 日 技术博客 2020 年 12 月 4 日 关于技术和电信公司的博客 2020 年 12 月 4 日`我们如何从给定的内容中删除“查看所有”文本`如何才能做到。解决方案
要访问文本节点,您必须检查nodeName
Element 的值是否等于"#text"
I 在这种情况下,我获取DIV
with 类的所有内容,并在获取具有 a 的元素后latest_posts
使用该属性访问它的所有子 节点。childNodes
querySelector
检索列表后,children
我可以遍历该列表并仅返回非类型的子项#text
let latest_posts = document.querySelector('.latest_posts');
let newContent = latest_posts.cloneNode();
newContent.innerHTML = "";
Array.from(latest_posts.childNodes).forEach(child => {
if(child.nodeName != "#text") {
newContent.append(child);
}
});
latest_posts.innerHTML = newContent.innerHTML;
<div class="latest_posts style3 latest_posts--style2 latest_posts2 clearfix eluidf4c60403 latestposts2--dark element-scheme--dark">
<h3 class="m_title m_title_ext text-custom latest_posts2-elm-title" itemprop="headline"></h3>
VIEW ALL
<ul class="posts latest_posts2-posts">
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/law-for-bank/" itemprop="headline">Law For Bank</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/tech-blog/" itemprop="headline">Tech Blog</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
<li class="post latest_posts2-post">
<h4 class="title latest_posts2-title"><a class="latest_posts2-title-link" href="http://wpstaging.a2zcreatorz.com/deloitte/design/blog-about-tech-and-telcos/" itemprop="headline">Blog About Tech and Telcos</a></h4>
<div class="latest_posts2-date"><span>December 4, 2020</span></div>
<div class="latest_posts2-itemSep clearfix"></div>
</li>
</ul>
<div>
推荐阅读
- javascript - 将视频锚点的所有实例替换为 iframe
- openiddict - 为所有 openiddict 路由指定 url 前缀
- c - 为什么 qemu 有时会比 ptrace 计数更多有时更少的指令?
- qt - 在 Qwt 图中使用一对一标签
- python - 如何在 2d 列表 python 中找到具有相同变量的值的总和,仅使用列表理解?
- haskell - 在模式匹配的不同情况下推导出不同的类型类约束
- javascript - 如何处理由崩溃导致的 404 页面返回。反应路由器
- swift - 停止在 UITableView 的嵌套 UICollectionView 中播放视频或仅在嵌套结构中的可见单元格上播放
- svelte - Svelte (rollup) - 错误:意外的令牌(请注意,您需要 @rollup/plugin-json 来导入 JSON 文件)
- datetime - 如果 yaxis 包含空 dateTime 数据并且 useUTC 为 false,则图表未呈现并引发脚本错误