将$(this)从函数传递给函数 [英] passing $(this) from function to function

查看:84
本文介绍了将$(this)从函数传递给函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在不同的课程上有两个不同的事件:

I have 2 different event on different classes :

$('.box1').click(function(){
$(this).find('something').css('red')
}

$('#otherId .box2').change(function(){
$(this).find('something').css('red')
}

但我计划将它们拆分为一个函数以避免重复代码

But I plan to split them out to a function to avoid duplicate code

function getDetails(this){
  this.find(".something").css('red');
}

但是如何稍后调用该函数?将$(this)传递给函数?

but how to call the function later? pass the $(this) to the function?

$('#otherId .box2').change(function(){
getDetails($(this))
}


推荐答案

是一个关键字,因此它不能是一个参数名称

this is a keyword so it can't be a param name

function getDetails(el) {
    el.find(".something").css('red');
}

$('#otherId .box2').change(function () {
    getDetails($(this))
})

这篇关于将$(this)从函数传递给函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆