Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript HTML多个事件来触发相同的函数_Javascript_Jquery_Html - Fatal编程技术网

Javascript HTML多个事件来触发相同的函数

Javascript HTML多个事件来触发相同的函数,javascript,jquery,html,Javascript,Jquery,Html,有没有办法让多个事件(例如oninput、onblur)触发HTML中完全相同的函数 这是我试图简化的HTML: <input id="Email" name="Email" type="text" oninput="toggleSuccessIcon(this, isEmail)" onblur="toggleSuccessIcon(this, isEmail)"> 我知道这在

有没有办法让多个事件(例如oninput、onblur)触发HTML中完全相同的函数

这是我试图简化的HTML:

<input id="Email" name="Email" type="text" oninput="toggleSuccessIcon(this, isEmail)" onblur="toggleSuccessIcon(this, isEmail)">

我知道这在jQuery中是可能的,但由于我有许多不同的输入(例如地址、邮政编码等),需要调用不同的函数(例如toggleSuccessIcon(this,isAddresss)、toggleSuccessIcon(this,isPostCode)等),我希望避免在JavaScript中进行冗长而混乱的初始化。然而,如果我在HTML而不是JQuery中这样做是愚蠢的,我希望能解释一下使用JQuery的优势


请注意,isEmail、isAddress、isPostCode等是一个函数名。

您可以使用helper函数

// events and args should be of type Array
function addMultipleListeners(element,events,handler,useCapture,args){
  if (!(events instanceof Array)){
    throw 'addMultipleListeners: '+
          'please supply an array of eventstrings '+
          '(like ["onblur","oninput"])';
  }
  //create a wrapper for to be able to use additional arguments
  var handlerFn = function(e){
    handler.apply(this, args && args instanceof Array ? args : []);
  }
  for (var i=0;i<events.length;i+=1){
    element.addEventListener(events[i],handlerFn,useCapture);
  }
}

function handler(e) {
  // do things
};

// usage
addMultipleListeners(document.getElementById('Email'),
                     ['oninput','onblur'],handler,false);
//事件和参数应为数组类型
函数addMultipleListeners(元素、事件、处理程序、useCapture、参数){
if(!(数组的事件实例)){
抛出“addMultipleListeners:”+
'请提供一个EventString数组'+
"(如"onblur","oninput")",;
}
//为创建包装,以便能够使用其他参数
var handlerFn=函数(e){
apply(这是数组的args&&args实例?args:[]);
}

对于(var i=0;i,您可以使用
数据
作为:

<input class="configurable-events" type="text" data-events="blur focus click" data-function="myFunction" />
<input class="configurable-events" type="password" data-events="blur focus" data-function="myPasswordFunction" />

我相信把js和htmlYou混合在一起会更混乱。你可以通过JavaScript中的
addEventListener
附加事件,或者使用jQuery以更干净的方式附加事件。所有输入实际上都使用“ToggleSuccessCon”,但第二个和可选的第三个参数因输入而异,例如,如果我想向所有输入添加相同的函数,这将非常好,但是toggleSuccessIcon的第二个参数是一个与输入字段相关的参数。答案仅取决于您询问的内容。如果您只想更改此输入字段的参数,请$(“#isEmail”)而不是$(“input”)。我不明白您所说的第二个参数是什么意思。您将始终使用$(this.attr('id')从选择器获取id。
$('.configurable-events').each(function(){
    $(this).on($(this).data('events'), function(){
      $(this).data('function')($(this));
    });
});

function myFunction(myInput) {
  console.log(myInput.value());
}

function myPasswordFunction(myPasswordInput) {
  console.log(myPasswordInput.size());
}
$("input").on( "click blur", toggleSuccessIcon(this, isEmail));