将onclick事件中的"this"传递给JQuery

Vic*_*r 1 javascript jquery

我想删除onclick=togglePass(this)我的锚标签,并希望在jQuery中使用它.我很困惑如何将this参数发送给jQuery?

$('elem').click(function());

现在我正在尝试onclick用以下内容替换:

$('elem').click(function(){..Some code..});

但不知道如何通过this.

Mam*_*mun 5

您不需要this显式传递. this自动绑定到触发事件的隐式元素.

请尝试以下示例:

$(".elem").click(function(){
  // this referes to the currently clicked element
  $(this).toggleClass('blue');
});
Run Code Online (Sandbox Code Playgroud)
.elem{
  background: green;
  width: 200px;
  height:200px;
  border-radius: 3px;
}
.blue{
  background: blue;
}
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="elem">Container</div>
Run Code Online (Sandbox Code Playgroud)