vue获取指定元素

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

vue获取指定元素

MrLi-2018   2022-06-03 我要评论

如何获取指定元素

在想要获取元素上添加“ref”  ref="scroll"  scroll为函数名

<div ref="scroll">vue获取指定元素</div>
 xiaFn:function(){
    console.log(this.$refs.scroll)
 }

点击获取相应元素

在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'));
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们