vue中點擊獲取相應元素


在vue中通過點擊事件獲取上一個標簽、父標簽、第一個子標簽等元素。( 以下元素都是以所點擊的元素進行查找 )

  • e.target 獲取當前點擊的元素
  • e.currentTarget 獲取綁定事件的元素
  • e.currentTarget.previousElementSibling 獲取前(上)一個元素
  • e.currentTarget.parentElement 獲取父元素
  • e.currentTarget.firstElementChild 獲取第一個子元素
  • e.currentTarget.nextElementSibling 獲取后(下)一個元素
  • e.currentTarget.getAttributeNode('class') 獲得點擊元素的class屬性
<div class="box_home">
  box_home
  <div class="box_pre">box_pre</div>
  <div class="box" @click="eleclick($event)">
    <div class="box_item">box_item</div>
    <div class="box_item2">box_item2</div>
  </div>
  <div class="box_next">box_next</div>
</div>


eleclick(e){
  console.log("當前點擊的元素");
  console.log(e.target);
  console.log("上一個標簽");
  console.log(e.currentTarget.previousElementSibling);
  console.log("父標簽");
  console.log(e.currentTarget.parentElement);
  console.log("第一個子標簽");
  console.log(e.currentTarget.firstElementChild);
  console.log("下一個標簽");
  console.log(e.currentTarget.nextElementSibling);
  console.log("綁定事件的標簽");
  console.log(e.currentTarget);
  console.log("獲得點擊元素的class屬性");
  console.log(e.currentTarget.getAttributeNode('class'));
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM