我在不同的类(class)有 2 个不同的事件:
$('.box1').click(function(){
$(this).find('something').css('red')
}
$('#otherId .box2').change(function(){
$(this).find('something').css('red')
}
但我打算把它们拆分成一个函数来避免重复代码
function getDetails(this){
this.find(".something").css('red');
}
但是后面怎么调用这个函数呢?将 $(this) 传递给函数?
$('#otherId .box2').change(function(){
getDetails($(this))
}
最佳答案
this
是关键字,所以不能是参数名
function getDetails(el) {
el.find(".something").css('red');
}
$('#otherId .box2').change(function () {
getDetails($(this))
})
关于javascript - 将 $(this) 从一个函数传递到另一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31331793/