简体   繁体   English

如何在不使用Javascript调用参数的情况下将参数传递给引用函数

[英]How do I pass an argument to referenced function without invoking it using Javascript

I want my code to be something like el.onclick = someFunc(arg) , however, someFunc is invoked on load. 我希望我的代码类似于el.onclick = someFunc(arg) ,但是,在加载时调用someFunc。 How would I go about passing an argument to a function that I want to reference, without also invoking it? 我该如何在不调用参数的情况下将参数传递给要引用的函数呢?

Try using the addEventListener to add the event click. 尝试使用addEventListener添加事件单击。 The addEventListener method attaches an event handler to the specified element. addEventListener方法将事件处理程序附加到指定的元素。 You need to pass the callback, which would be invoked when the event occurs. 您需要传递回调,事件发生时将调用该回调。

movieElement.addEventListener("click", function(){
    showDescription(arg);
});

You can do this in following 3 ways, 您可以通过以下3种方式进行操作:

  1. Using addEventListener 使用addEventListener

 var elm = document.getElementById('testButton'); elm.addEventListener('click', function(){ someFunc('testArg'); }); function someFunc(arg) { console.log(arg); } 
 <input id="testButton" type="button" value="Click" /> 

  1. Using onclick and bind 使用onclickbind

 function someFunc(arg) { console.log(arg); } var elm = document.getElementById('testButton'); elm.onclick = someFunc.bind(null, 'testArg'); 
 <input id="testButton" type="button" value="Click" /> 

  1. Using onclick and function wrapper, 使用onclick和函数包装器,

 function someFunc(arg) { return function() { console.log(arg); } } var elm = document.getElementById('testButton'); elm.onclick = someFunc('testArg'); 
 <input id="testButton" type="button" value="Click" /> 

Note : Option 1 with addEventListener is the best one to use among the options above but other options (1 & 2) can be considered in case of option 1 is not doable. 注意 :在上述选项中,带有addEventListener选项1是最好的选择,但在选项1无效的情况下,可以考虑其他选项(1&2)。

For an instance, you can use onclick and bind when you need to reuse the same method for multiple elements and have to use the element inside the method with this as below. 对于一个实例,可以使用onclickbind时需要重用多个元素相同的方法,并具有使用该方法内部的元件与this如下。

 var buttons = document.getElementsByTagName('input'); for(var i = 0; i < buttons.length; i++) { var button = buttons[i]; button.onclick = someFunc.bind(button, 'testArg'); } // Assume this function is in another file function someFunc(arg) { console.log(this.id + ': '+ arg); } 
 <div> <input id="testButton1" type="button" value="Click" /> <input id="testButton2" type="button" value="Click" /> <input id="testButton3" type="button" value="Click" /> <input id="testButton4" type="button" value="Click" /> <input id="testButton5" type="button" value="Click" /> </div> 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不调用函数的情况下将函数作为对象的一部分传递给javascript - how to pass a function as part of an object in javascript without invoking the function 如何将参数传递给匿名Javascript函数? - How do I pass argument to anonymous Javascript function? 如何将 HTML 元素作为参数传递给 Javascript 函数? - How do I pass an HTML element as an argument to a Javascript function? 如何在不手动调用 javascript 中的 function 的情况下使用 ajax 自动刷新页面的一部分? - How do I use ajax to auto refresh a section of page without manually invoking a function within javascript? 我该如何在其他函数中调用自调用javascript函数? - How do i recall self invoking javascript function in a other function? JavaScript:如何将字符串参数传递给函数以用于按该字符串参数的值对对象数组进行排序? - JavaScript: How do I pass a string argument to a function to use to sort an array of objects by the value of that string argument? 如何在此 function 中传递参数 - How do I pass a argument in this function 如何获取正在调用javascript函数的文本框的ID? - How do I get the id of a textbox that is invoking a javascript function? 在JavaScript中,如何使用字符串参数访问对象上的函数? - In JavaScript, how do I access a function on an object using a string argument? 如何使用Javascript将数组vlaue从函数中传递出去 - How do I pass an Array vlaue out of a function using Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM