• vue 点击当前元素进行显示隐藏,上次点过的元素隐藏


     功能描述:当一直点击2时,切换显示隐藏, 当2展开时,在点击1时,2隐藏,1显示。

    1. <ul id="mttUl" class="mttUl">
    2. <li v-for="(item,index) in aboutData.mttImg" :key="index">
    3. <!-- -->
    4. <img :src="item.img" :alt="item.name" @click="stretchFn($event,index)">
    5. </li>
    6. </ul>
    7. <script>
    8. export default {
    9. data(){
    10. return {
    11. //图片点击切换
    12. active:0,
    13. removeAc:-1,
    14. isStatus:false, //判断当前是展开还是收回
    15. currentIndex:-1, //获取当前下标
    16. switchEle:0, //切换点击元素
    17. }
    18. },
    19. methods: {
    20. stretchFn($event,index){
    21. var getEle = document.querySelectorAll('.mttUl li'); //获取所有的li元素;
    22. if(this.switchEle != index){
    23. getEle[this.switchEle].classList.remove('active');
    24. }
    25. for(let i = 0 ; i < getEle.length; i++){
    26. if(getEle[index].classList.contains('active')){ //是否含有active
    27. getEle[index].classList.remove('active') //移除active
    28. }else{
    29. getEle[index].classList.add('active'); //添加active
    30. this.switchEle = index;
    31. }
    32. }
    33. }
    34. }
    35. }
    36. </script>

    补充知识点:

    $event 可以获取当前点击标签信息内容 

    $event.currentTarget.parentElement.getAttribute('class')       //获取当前标签父元素class

    $event.currentTarget.parentElement.parentElement;  //获取当前元素父元素的父元素

    $event.currentTarget.parentElement.children;      //获取当前元素父元素下的子元素

    document.querySelectorAll('.mttUl li')             //获取所有的指定类名元素

    getEle[index].classList.contains('active')      // 判断当前元素是否含有某个类

    getEle[index].classList.remove('active')        // 当前元素移除某个类

    getEle[index].classList.add('active');           //当前元素添加某个类

    • e.target 获取当前点击的元素
    • e.currentTarget 获取绑定事件的元素
    • e.currentTarget.previousElementSibling 获取前(上)一个元素
    • e.currentTarget.parentElement 获取父元素
    • e.currentTarget.firstElementChild 获取第一个子元素
    • e.currentTarget.nextElementSibling 获取后(下)一个元素
    • e.currentTarget.getAttributeNode('class') 获得点击元素的class属性

  • 相关阅读:
    pandas reindex 方法
    2023年B题人工智能对大学生学习影响的评价
    计算一个数的N次方,int和Integer的区别
    1.4_5 Axure RP 9 for mac 高保真原型图 - 案例4 【旋转的唱片2】计时器
    【WordPress】在 Ubuntu 系统上使用 Caddy 服务器来发布 WordPress 网站
    探索跑腿配送App的未来:技术和创新的前沿
    前端vue实战项目结构、常用编辑器vs code 配置
    计算机网络第八章知识点回顾(自顶向下)
    LVS-NAT模式部署
    2022年SQL经典面试题总结(带解析)
  • 原文地址:https://blog.csdn.net/qq_30607437/article/details/127107779