我有这样的事情:
$('element.selector').live("click", function (){
run_some_func ();
});
$('element.selector2').live("click", function (){
run_some_func ();
});
Run Code Online (Sandbox Code Playgroud)
现在在函数中我需要使用 $(this):
function run_some_func () {
$(this).show();
}
Run Code Online (Sandbox Code Playgroud)
如何让函数知道 $(this) 是被单击的 element.selector?
谢谢。
您可以使用call函数来更改this要执行的函数的上下文(设置关键字):
$('element.selector').live("click", function (){
run_some_func.call(this); // change the context of run_some_func
});
function run_some_func () {
// the this keyword will be the element that triggered the event
}
Run Code Online (Sandbox Code Playgroud)
如果您需要向该函数传递一些参数,您可以:
run_some_func.call(this, arg1, arg2, arg3); // call run_some_func(arg1,arg2,arg3)
// and change the context (this)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
906 次 |
| 最近记录: |