gpt4 book ai didi

javascript - 单击 IMG 使用 Jquery $(this) 定位其中的 DIV。 $(this) 是被点击的图像

转载 作者:太空宇宙 更新时间:2023-11-03 20:15:34 24 4
gpt4 key购买 nike

我想点击我的 IMG 标签,让它使用 Jquery 选择它下面的最后一个 DIV 标签,类是“comment-text”

  <div class="one-comment hill">
<h4 class="name-title">Durdan Moraan</h4>
<**img** src="pencil-grey.png" class="edit-icon">
<div class="date-box">
<div class="month">
Mar
</div>
<div class="day">
27
</div>
<div class="time">
11:11 am
</div>
</div>
**<div class="comment-text">**
Ok this is the comment.
**</div>**
</div>

最佳答案

您可以使用 siblings()因为 .comment-text 是图像的兄弟。或者你可以使用

$('img.edit-icon').click(function(){
var commentTextDiv = $(this).siblings('.comment-text:last'); //select last .comment-text div
var commentText = commentTextDiv.html();
});

DEMO

您可以使用 .nextAll()

$('img.edit-icon').click(function () {

var commentTextDiv = $(this).nextAll('.comment-text:last'); //select div
var commentText = commentTextDiv.html();
alert(commentText)
});

DEMO 2

关于javascript - 单击 IMG 使用 Jquery $(this) 定位其中的 DIV。 $(this) 是被点击的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21937956/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com