The .closest và .find selectors are complements of each other & used together are the best way to lớn get to the corresponding element of where the click (or any event) occurred.
Bạn đang xem: Working of jquery prev() function
From the documentation (paraphrased):
The .closest selector traverses up the DOM to lớn find the parent that matches the conditions. the .find selector traverses down the DOM where the event occurred, that matches the conditions.Now, given these and since the sự kiện is always passed as the attribute lớn the bound function, following is the simplest, best method khổng lồ get lớn the corresponding element somewhere:
data = $(event.target).closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).find("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.title"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg); If you got what I am saying, that is all. But I will try to explain it with an example for those it is not already clear.
Let"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgs consider the following HTML; & let"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgs say you want the title nội dung on clicking Send email.
Xem thêm: Bí Quyết Đọc Sách Tiếng Anh Hiệu Quả Nhờ Đọc Sách, Kinh Nghiệm Không Bỏ Cuộc Khi Đọc Sách Tiếng Anh
The simplest way to lớn get the title on clicking the button, would be the following:
title = $(event.target).closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).find("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.title"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg); of course, where the "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.descr-button"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg is bound to the function in the $(document.ready)
$("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgbody"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg).on("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgclick"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg, "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.descr-button"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg, do_something); Related protips: fatal: refusing lớn merge unrelated histories
Typo. "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgelement-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg)"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg should be "https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.closest("https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.element-row"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg)"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpg.
closest actually exists in native javascript now, and querySelectorAll behaves like find. If you"https://obatambeienwasirherbal.com/find-previous-div-with-class-jquery/imager_1_22385_700.jpgre using jQuery youre probably interested in.siblings(