javascript - 如何从点击目标中获取上一个元素?
问题描述
你好我有这个html代码:
<div class="row newrow">
<div class="col-10"><b>this</b></div>
<div class="col-2">
<img src="close.png" id="exit"/>
</div>
</div>
当我使用此代码单击带有 id 退出的 img
$('body').on('click','#exit',function(e){
})
我需要得到<b>
它背后的文字,这将是"this"
我已经尝试过了,但它不起作用:
$('body').on('click','#exit',function(e){
$q = $(e.target).prev('b')
var word = $q.text()
)}
它只给了我从一开始就点击的那个
解决方案
试试这个:
$('body').on('click','#exit',function(e){
var this_b = $(this).parent().prev().children(0).html();// get the text
alert(this_b);
});
推荐阅读
- python - Python中基于多列垂直展平多列并合并
- powershell - 如何使用powershell检查重复的多个文件?
- django - 如何在 django 中创建 2 级管理员用户,每个用户将只管理他创建的用户?
- if-statement - Vlookup 然后在 Google 表格中返回不同的行
- c++ - 由于某种原因,我的代码中出现了 [json.exception.type_error.302] 。我知道错误是什么意思,但我不知道哪里有问题
- php - 为什么在 YII2 活动表单中没有定义 $model 变量时出现错误
- matlab - MATLAB:x 轴上绘制的是什么?
- android - 如何从传感器 TYPE_ROTATION_VECTOR 获得的 3D 矩阵计算 2D 旋转(锁定一个轴旋转)
- matlab - 为什么对于这个看似有效的变换矩阵,我会收到警告 MATLAB:hg:DiceyTransformMatrix?
- python-3.x - 当我用 utf-8 读取文件时,.find() 不起作用