gpt4 book ai didi

javascript - 将 "this"从 onclick 事件传递到 JQuery

转载 作者:行者123 更新时间:2023-11-30 07:52:58 25 4
gpt4 key购买 nike

我想从我的 anchor 标记中删除 onclick=togglePass(this) 并想在 jQuery 中使用它。我很困惑如何将 this 作为参数发送给 jQuery?

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

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

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

但不确定如何传递this

最佳答案

您不需要显式传递 thisthis 自动绑定(bind)到触发事件的元素。

试试下面的例子:

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

<div class="elem">Container</div>

关于javascript - 将 "this"从 onclick 事件传递到 JQuery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48293472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com