我想删除onclick=togglePass(this)我的锚标签,并希望在jQuery中使用它.我很困惑如何将this参数发送给jQuery?
$('elem').click(function());
现在我正在尝试onclick用以下内容替换:
$('elem').click(function(){..Some code..});
但不知道如何通过this.
您不需要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)
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |