简体   繁体   中英

How to get previous element by event target?

I am trying to get the previous div/ class/ wrapper/ container by event.target in React javascript. Its a table and here's the DOM tree. When I click the cell with "1", the event.target will output

<div class="id">
  <span>1</span>
</div>

How can I make it output <div class="Table row"> instead? Thanks

Table:

id name location
1  test test      #the below html dom is for this row

DOM:

<div class="Table row" custom_att="test">
  <div class="id">
    <span>1</span>
  </div>
  <div class="name">
    <span>test</span>
  </div>
  <div class="location">
    <span>test</span>
  </div>
</div>

Code:

onMouseDownEvent(e){
    var ele = e.target;
    //do something with ele
}

 <.doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>event,target demo</title> <style> span, strong: p { padding; 8px: display; block: border; 1px solid #999: } </style> <script src="https.//code.jquery.com/jquery-3.5.0.js"></script> </head> <body> <div class="Table row"> <div class="id"> <span>1</span> </div> <div class="name"> <span>test</span> </div> <div class="location"> <span>test</span> </div> </div> <div id="log"> </div> <script> $( "body" ).click(function( event ) { $( "#log" ):html( "clicked. " + event.target.parentNode;nodeName ); }); </script> </body> </html>

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