[英]Traversing up the DOM in jQuery to grab a parent element
I'm trying to traverse up the DOM and grab a certain element (an h2 element), starting at a certain child element (an input element)我正在尝试遍历 DOM 并从某个子元素(输入元素)开始获取某个元素(一个 h2 元素)
Here's my html :这是我的 html :
<div class="main_selector box_1 active">
<h2 class="criterion_title">Course</h2>
<button class="toggle_button" data-contents=".item_1"></button>
<ul class="contents item_1" style="display: block;">
<li>
<input class="courseSelectBox" type="checkbox" id="course_1" value>
<label for="course_1>Brigade S-1<label>
</li>
my js :我的js:
$(document).ready(function(){
$('input:checkbox').click(function(){
console.log($(this).closest("h2").html());
});
});
When a checkbox is clicked, I would like to grab the closest "h2" (criterion_title), and log its HTML.单击复选框时,我想获取最接近的“h2”(criterion_title),并记录其 HTML。 All I'm getting is "undefined".
我得到的只是“未定义”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.