我有一个这样的功能。我想选择当前元素而不传递参数或更改 HTML 中的任何内容。
我看到 $(this)
不起作用。我们还有其他技术可以实现这一目标吗?
function social_follow_us(){
$(this).addClass("green")
}
像这样的 HTML。
<a href="..." onclick="social_follow_us()" class="btn btn-primary followButton" target="_blank" id="instagram_follow">Follow on Instagram</a>
最佳答案
另一个解决方案是传递 this
来自<a onclick="...">
事件:
function social_follow_us(elem) {
$(elem).addClass("green")
}
a.green {
color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="..." onclick="social_follow_us(this)" class="btn btn-primary followButton" target="_blank" id="instagram_follow">Follow on Instagram</a>
关于javascript - 触发当前定义 onclick 事件的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49948034/