如何传递参数给addEventListener监听器函数?

编程入门 行业动态 更新时间:2024-10-23 03:32:08
本文介绍了如何传递参数给addEventListener监听器函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

情况有点像 -

var someVar = some_other_function(); someObj.addEventListener("click", function(){ some_function(someVar); }, false);

问题是 someVar 在 addEventListener 的监听器函数内不可见,在这个函数可能被视为一个新变量。

The problem is that the value of someVar is not visible inside the listener function of the addEventListener, where it is probably being treated as a new variable.

推荐答案

你写的代码完全没有错。应该可以访问 some_function 和 someVar ,以防在匿名

There is absolutely nothing wrong with the code you've written. Both some_function and someVar should be accessible, in case they were available in the context where anonymous

function() { some_function(someVar); }

已创建。

检查警报是否提供您一直在寻找的价值,请确保它在匿名功能的范围内可以访问(除非您有更多的代码可以运行在调用 addEventListener 之前的同一个 someVar 变量上)

Check if the alert gives you the value you've been looking for, be sure it will be accessible in the scope of anonymous function (unless you have more code that operates on the same someVar variable next to the call to addEventListener)

var someVar; someVar = some_other_function(); alert(someVar); someObj.addEventListener("click", function(){ some_function(someVar); }, false);

更多推荐

如何传递参数给addEventListener监听器函数?

本文发布于:2023-10-31 06:03:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545227.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监听器   函数   参数   addEventListener

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!