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

问题描述 投票:0回答:1

我正在尝试在React javascript中通过div/ class/ wrapper/ container获得先前的event.target。它是一张桌子,这里是DOM树。当我单击带有“ 1”的单元格时,event.target将输出

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

如何使它输出<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
}
javascript html xml dom element
1个回答
0
投票

<!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>
© www.soinside.com 2019 - 2024. All rights reserved.