<a href="javascript:void(0);" onclick="SendClass(this);">Toggle class</a>
<script type="text/javascript">
function SendClass(sender)
{ console.log(sender);
}
</script>
我可以通过 onclick="SendClass(this)
将 this
发送到函数 SendClass(sender),并将 event
发送到函数 SendClass(sender) )通过 onclick="SendClass(event)
,所以我的问题是:
1.为什么我们可以将参数this
和event
传递给function SendClass(sender)
?
2.我还可以传递给函数SendClass(sender)
使用哪些参数?
最佳答案
1.why we can pass parameters this and event to function SendClass(sender)?
onxyz
属性中的代码在具有 this
(元素)和 event
事件的上下文中运行。因此,您可以在该代码中使用这两个方法,包括将它们传递到您调用的函数中。
2.what parameters else I can pass to function SendClass(sender) to be used?
这几乎是上下文定义的唯一两个有用的。当然,您可以传递任何您喜欢的其他内容,但这些是上下文定义的两个最有用的内容。
关于javascript - 在 jQuery 中传递参数的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17521057/