繁体   English   中英

如何通过事件目标获取上一个元素?

[英]How to get previous element by event target?

我正在尝试通过 React javascript 中的event.target获取以前的div/ class/ wrapper/ container 它是一张表,这里是DOM树。 当我单击带有“1”的单元格时,event.target 将为 output

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

如何改为 output <div class="Table row"> 谢谢

桌子:

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>

代码:

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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