zoukankan      html  css  js  c++  java
  • jquery与箭头函数的联系

    用jquery写一个按钮点击事件很简单的吧,选中这个点击的按钮也很简单,直接$(this)就可以选中了。

    //html
    <button class="btn">点击一下嘛</button>
    //js
    // 普通函数
    $('.btn').click(function(){
    	alert('我要消失啦')
    	$(this).hide();
    	console.log($(this));  //选中了当前选中的jquery元素,也就是btn
    })
    

    那用箭头函数如何才能选中当前点击的按钮呢?

    $('.btn').click((e)=>{
    	console.log(this);              //Window {parent: Window, opener: null, top: Window, length: 0, frames: Window, …}
            console.log($(this));           //init [Window]
    })
    

    这样并不可以的!

    我们来参考vue

    首先 vue的点击事件 是用 @click = “clickfun()” 属性 在html中绑定的,
    在点击的函数中 添加$event 参数就可以

    <button  @click = “clickfun($event)”>点击</button>
    			
    methods: {
    	clickfun(e) {
    	// e.target 是你当前点击的元素
    	// e.currentTarget 是你绑定事件的元素
            }
    },

    参考链接:https://blog.csdn.net/webfront/java/article/details/80310763

    所以:

    // 箭头函数
    $('.btn').click((e)=>{
    	console.log(e.target);              //选中了当前选中的那个dom元素,也就是btn
    	console.log($(e.target));           //选中了当前选中的jquery元素,也就是btn
    	console.log(e.currentTarget);       //选中了当前选中的那个dom元素,也就是btn
    	console.log($(e.currentTarget));    //选中了当前选中的jquery元素,也就是btn
    })
    

    这样完美解决,一般用e.target。

  • 相关阅读:
    DNS 服务器的配置与管理
    为什么苹果不再需要谷歌地图?
    flash安装时提示无法安装解决方法
    苹果新ipad支持siri吗?答案是不支持!
    HTTP的 Basic 验证
    笑解优酷土豆合并
    crontab简介
    循环链表应用
    计算表达式
    走迷宫 dfs
  • 原文地址:https://www.cnblogs.com/pengxiangchong/p/12727942.html
Copyright © 2011-2022 走看看