var funcs = [];
// let's create 3 functions
for (var i = 0; i < 3; i++) {
// and store them in funcs
funcs[i] = function() {
// each should log its value.
console.log("My value: " + i);
};
}
for (var j = 0; j < 3; j++) {
// and now let's run each one to see
funcs[j]();
}Run Code Online (Sandbox Code Playgroud)
它输出这个:
我的价值:3
我的价值:3
我的价值:3
而我希望它输出:
我的价值:0
我的价值:1
我的价值:2
使用事件侦听器导致运行函数的延迟时,会出现同样的问题:
var buttons = document.getElementsByTagName("button");
// let's create 3 …Run Code Online (Sandbox Code Playgroud)我如何通过传递一些参数(在这种情况下一个整数)传递给函数addEventListener在一个click事件?
我有两个按钮; 如果我按下正确的一个,我想做一些东西,如果我按左边的那个,那么我希望它做其他事情.
这是代码:
document.getElementById('date_right').addEventListener( 'click', switch_date(1), false );
document.getElementById('date_left').addEventListener( 'click', switch_date(-1), false );
function switch_date( k )
{
if(k==1)
{
//do stuff
}
else
{
//another stuff
}
}
Run Code Online (Sandbox Code Playgroud) 我似乎无法删除接收器事件侦听器.鉴于控制台下面的代码将继续无休止地打印" hi ".
接收者:
window.addEventListener("message", function(e){
console.log('hi');
window.removeEventListener("message", function(e){}, false)
}, false);
Run Code Online (Sandbox Code Playgroud)
发件人:
var emiter = setInterval(function(){
console.log('sending message');
window.parent.postMessage( messageData, "*" );
}, 1000);
Run Code Online (Sandbox Code Playgroud)
有没有解决的办法 ?
问题:我想通过使用按钮的click事件传递参数来调用函数"test".
使用addEventListener ('click', test );作品.
使用addEventListener ('click', test(parameter));该功能在加载时自动激活,而不是单击按钮.
问题:如何传递参数?
//create 3 button (n is a counter):
var btn = document.createElement("INPUT");
btn.setAttribute("id", "demo"+n);
btn.setAttribute("type", "button");
btn.setAttribute("value", ("click"));
document.body.appendChild(btn);
Run Code Online (Sandbox Code Playgroud)
呼叫没有参数工作正确:
function test(m) {
alert("YOU CLICKED ME!");
}
var m=0;
while(m!=3){
document.getElementById("demo"+m).addEventListener("click", test);
m=m+1;
}
Run Code Online (Sandbox Code Playgroud)
`
参数不起作用(该功能立即激活):
function test(m) {
alert("YOU CLICKED ME!"+m);
}
var m=0;
while(m!=3){
document.getElementById("demo"+m).addEventListener("click", test(m));
m=m+1;
}
Run Code Online (Sandbox Code Playgroud) 我正在玩Javascript并创建了一个类.在初始化类时,我向某个div添加一个按钮,然后在单击该按钮时向该按钮添加一个事件监听器.但是,当页面加载时,函数会被触发,而不是在单击按钮时触发.这是我的代码:
function Photobank(){
this.photos;
this.addPhotoButton;
}
Photobank.prototype.init = function(){
var btn = document.createElement('button');
btn.id = "photoupload";
var t=document.createTextNode("ADD PHOTOS");
btn.appendChild(t);
this.addPhotoButton = btn;
var pb = document.getElementById('photobank');
pb.appendChild(this.addPhotoButton);
this.addPhotoButton.addEventListener("click", this.launchMediaManager(), false);
}
Photobank.prototype.launchMediaManager = function(){
alert("Launching Media Manager");
}
Run Code Online (Sandbox Code Playgroud)
我做了明显错误的事吗?
我想将参数传递给 Javascript 中的事件侦听器。我找到了解决方案,但我无法理解它们为什么或如何工作以及为什么其他解决方案不起作用。
我有 C/C++ 背景,但是 Javascript 函数的执行有很大不同。您能否帮助我理解以下示例如何以及为何有效或无效?
没有参数,代码可以正常工作:
var clicker = document.getElementById("mainNavToggle");
clicker.addEventListener("click", eventFunction);
function eventFunction()
{
console.log("works");
}
Run Code Online (Sandbox Code Playgroud)
如果我在 eventListener 上包含括号,该函数将在没有事件触发的情况下执行一次,然后不执行任何操作:
var clicker = document.getElementById("mainNavToggle");
clicker.addEventListener("click", eventFunction());
function eventFunction()
{
console.log("works");
}
Run Code Online (Sandbox Code Playgroud)
我想这是因为该函数是在 eventListener 处调用的,并且这不允许 eventListener 函数稍后调用 eventFunction。
当我想用 eventFunction 传递参数时。以下不起作用:
var clicker = document.getElementById("mainNavToggle");
clicker.addEventListener("click", eventFunction("works"));
function eventFunction(myStr)
{
console.log(myStr);
}
Run Code Online (Sandbox Code Playgroud)
它在不触发事件的情况下调用 eventFunction,然后当事件触发时不执行任何操作,这与之前的行为相同。到目前为止我想我能理解。
为了在事件函数中正确传递参数,我找到了以下代码。以下解决方案工作正常:
var clicker = document.getElementById("mainNavToggle");
clicker.addEventListener("click", eventFunction("works"));
function eventFunction(myStr)
{
function func(event, myStr)
{
console.log(myStr);
}
return function(event) {
func(event,myStr)
};
}
Run Code Online (Sandbox Code Playgroud)
我注意到的第一件事是这个 eventFunction …