jquery - 将 div 文本添加到数组
问题描述
我需要将此<div><a href="">Test</a>and another <a href="">Test</a></div>
文本添加到这样的数组中:['Test', 'and another', 'Test']
,但结果是['Test', 'Test']
,“和另一个”文本被忽略。
let text = [];
$('div').children().each(function () {
text.push($(this).text());
});
解决方案
正如这里所解释的
.children() 只返回 html 元素而不是文本,所以很明显,另一个不包含在项目中。为了达到您想要的结果,您可以简单地将它放在<span>
标签中:)
推荐阅读
- laravel - 如何在 Laravel 中只显示有数据的列?
- sql - 如何在 SQL 中按数字排序?
- python - 读取 CSV 并将其呈现为显示为 Datatables 不工作
- excel - 将选定单元格的特定区域选择为 PDF,然后在工作表中使用 VBA 进行打印
- flutter - Flutter 中的 delta、globalPosition、localPosition 和 primaryDelta 是什么?
- python - 如何在 Python 中保存对文件的响应?
- javascript - 将平面列表渲染“项目”传递给函数。React Native(有点 js 问题)
- javascript - 存储/重用 Ajax 响应的最佳实践
- azure-devops - 在 Microsoft 机器学习服务器上使用 Azure Devops python 工件存储库
- docker - 为什么我无法通过 tomcat 服务器访问我在 Kubernetes 集群中的应用程序?