简体   繁体   中英

How do I target the parent of a jquery selector?

I have a table of data that operates as a time tracker. Each row is a task. By clicking a box of that row, it starts and stops a stopwatch. However, with my current setup, $(".col-6"), I select the entire column so everything changes when I want only the row to change. My plan is to target the parent of the jquery selector, something like $("this.parent() .col-6"). I believe the parent is the recordId, which operates as an Id in the DOM. What is the proper syntax for this?

When you select a DOM node in javascript you can use the parentElement property that is afforded to all DOM nodes to access the parent element. In this case

$('.col-6).click(function() {
    console.log(this.parentElement.classList);
})

would (on click) log the class list of the parent element.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM