我想知道是什么之间的差异$(this),并this在jQuery,并最终找到一种方式来获得$(this)从对象this(this是什么?):
var last_btn;
$("#element").click (function () {
if (last_btn != null && last_btn == this) {
// to unselect the current button
last_btn.removeClass ("selected"); // doesn't work because this is not $(this)
} else {
if (last_btn != null) last_btn.removeClass ("selected"); // to unselect another old button
last_btn = this;
$(this).addClass ("selected");
}
});
Run Code Online (Sandbox Code Playgroud)
正如本文所述,我需要使用this而不是$(this)对象,因为这是将jQuery对象分配给var而不会松散其实例的唯一方法.
我怎样才能做到这一点?
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |