[英]How to find the closest sibling in jQuery
I am trying to learn jQuery, I have following mark up我正在尝试学习 jQuery,我有以下标记
In a div, I have two texts date and description and two buttons edit and delete.在一个 div 中,我有两个文本日期和描述以及两个编辑和删除按钮。
When I click the edit button, I want to get the date and description of that div当我单击编辑按钮时,我想获取该 div 的日期和描述
Here I am trying to get it using parents()
selector, how can I use closest()
selector here , if it is not possible with the current markup please suggest how can I proceed with closest()
selector.在这里,我试图使用
parents()
选择器来获取它,我如何在这里使用closest()
选择器,如果当前标记无法使用,请建议我如何继续使用closest()
选择器。
$(document).ready(function() { //If i click on edit button . I want to select the corresponding date and description .How can we do that? $(".taskTemplate .edit").on('click', function(event) { editTask(event.target); }); }); function editTask(node) { var date = $(node).parents('.taskTemplate').find('.date').html(); alert(date); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="taskTemplate"> <span class="date">205-10-09</span> <span class="desc">description of task</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div> <div class="taskTemplate"> <span class="date">2015-11-19</span> <span class="desc">description of task2</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div>
Just replace parents()
by closest()
in :只需用
closest()
替换parents()
:
var date = $(node).closest('.taskTemplate').find('.date').html();
Hope this helps.希望这可以帮助。
$(document).ready(function() { $(".taskTemplate .edit").on('click', function(event) { editTask(event.target); }); }); function editTask(node) { var closest_div = $(node).closest('.taskTemplate'); var date = closest_div.find('.date').text(); var description = closest_div.find('.desc').text(); console.log(date, `|`, description); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="taskTemplate"> <span class="date">205-10-09</span> <span class="desc">description of task</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div> <div class="taskTemplate"> <span class="date">2015-11-19</span> <span class="desc">description of task2</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div>
When i click the edit button, i want to get the date and description of that div
当我单击编辑按钮时,我想获取该 div 的日期和描述
You can use .siblings()
to select .date
, .desc
siblings of .edit
element您可以使用
.siblings()
来选择.edit
元素的.date
、 .desc
兄弟姐妹
$(document).ready(function() { //If i click on edit button . I want to select the corresponding date and description .How can we do that? $(".taskTemplate .edit").on('click', function(event) { editTask($(this).siblings(".date, .desc")); }); }); function editTask(nodes) { var data = $.map(nodes, function(el) { return `${el.className}: ${el.textContent}` }).join(", "); alert(data); }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script> <div class="taskTemplate"> <span class="date">205-10-09</span> <span class="desc">description of task</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div> <div class="taskTemplate"> <span class="date">2015-11-19</span> <span class="desc">description of task2</span> <input type="button" class="edit" value="edit"> <input type="button" class="delete" value="delete"> <span class="done">done</span> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.