javascript - 使用“this”jquery 获取父母文本数据
问题描述
我的文档 HTML STRUCTURE 中有以下 HTML 结构
[![在此处输入图像描述][1]][1]
我需要获取info__meta-dates
文本,所以只有使用onClick
jQuery 中的事件的日期,这是我到目前为止的代码:
$("input[type=checkbox]").click(function (e) {
$(this)
.parent()
.parent()
.parent()
.find(".info__meta--dates")
.text()
);
});
但我也得到date
了嵌套范围内的文本。
解决方案
您可以clone
跨标签并使用从克隆元素中.remove()
删除mdc-typography--caption
,以便最终结果将只是您需要的文本
演示代码:
$("input[type=checkbox]").click(function(e) {
var texts = $(this).closest(".ev-info_meta").find(".info__meta--dates").clone(); //clone that span tag
texts.find('span.mdc-typography--caption').remove(); //remove elemnt with class mdc-typography--caption
console.log(texts.text().trim()); //show
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="ev-info_meta">
<span class="info__meta--dates"><span class="mdc-typography--caption">dates</span> fffff</span>
<div>
<input type="checkbox">
</div>
</div>
推荐阅读
- terminal - zsh:找不到命令:mkdir
- javascript - 首次单击后按钮的作用相反
- java - SonarQube 一直抱怨 Use try-with-resources 或在“finally”子句中关闭此“PreparedStatement”
- r - ggsave 不保存绘图
- sql - 在 PostgreSQL IF / THEN 语句中使用 DELETE FROM...RETURNING
- php - PhpStorm 或 localhost 中的结帐后 + Docker
- r - R - 结转 1 直到达到另一个 1
- sql-server - 包括每个客户的 companyID 并将其链接到其他表 SQL
- c++ - 模拟队列的链表堆栈不起作用
- android-studio - 我在使用 AndroidStudio 的“构建文件夹下的文件”中遇到错误