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

标签 javascript jquery html css

我想点击我的 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/

相关文章:

html - 在表头 <th> 元素上使用 flexbox

html - 为安全起见对 HTML 输入进行编码时,如何避免对 Ñ 或 ñ 等国际字符进行编码?

javascript - HTML5 Canvas : how to have an image and a shape on the canvas and shape is Drag n Droppable?

javascript - 按类别筛选 选择

javascript - 向 Promise.all() 添加一个 Promise

javascript - 在 OWL Carousel 当前项目上添加类

php - 实时显示在其他在线用户上?

javascript - 尝试根据这种情况创建一个数组,jquery或javascript

javascript - 在 Blazor Web Assembly 应用程序中显示本地计算机摄像头源

javascript - 在下拉列表更改事件上更新数据库