简体   繁体   English

如何在if语句中使用多个元素,多个事件但功能相同

[英]how to use multiple elements, multiple events, but same function in an if statement

Let's say I have: 假设我有:

if (event X happens on element A) execute Code R 如果(事件X发生在元素A上)执行代码R

if (event Y happens on element B) execute Code R 如果(事件Y发生在元素B上)执行代码R

In jQuery, instead of the 2 if statements, how do you have 1 if statement whereby: 在jQuery中,如何使用1条if语句代替2条if语句:

if (event X happens on element A) OR (event Y happens on element B) execute Code R 如果(事件X发生在元素A上)或(事件Y发生在元素B上)执行代码R

I've been looking at bind methods but haven't been able to find the solution to this. 我一直在寻找绑定方法,但未能找到解决方案。 It's like Google's search function: If you click on the 'search' button, it executes the search. 就像Google的搜索功能一样:如果您单击“搜索”按钮,它将执行搜索。 However, if you type something in the input box and press the 'enter' key, it also executes the search. 但是,如果您在输入框中键入内容并按“输入”键,它也会执行搜索。 Again, two different elements (button and input box), two different events (mouse click and key press), but same function (search)? 同样,两个不同的元素(按钮和输入框),两个不同的事件(鼠标单击和按键),但功能相同(搜索)?

There won't be any if statements in your final code. 您的最终代码中将没有if语句。 You have to bind the event handler for each event to each element, and the event handler can of course be the same function: 您必须将每个事件的事件处理程序绑定到每个元素,并且该事件处理程序当然可以具有相同的功能:

function event_handler() {
    // ...
}

$(elementA).on('eventX', event_handler);
$(elementB).on('eventY', event_handler);

I recommend to read the jQuery tutorials: http://docs.jquery.com/Tutorials . 我建议阅读jQuery教程: http : //docs.jquery.com/Tutorials

To learn more about event handling in general, have a look at these articles: http://www.quirksmode.org/js/introevents.html . 要大致了解有关事件处理的更多信息,请阅读以下文章: http : //www.quirksmode.org/js/introevents.html

You'll need to add the same function to both event / element combinations: 您需要为两个事件/元素组合添加相同的功能:

$('#elementA').on('click', myEventHandler);
// You can specify multiple Events:
$('#elementB').on('mousedown mouseup blur', myEventHandler);
// Or specify multiple elements:
$('#elementB, #elementC').on('click', myEventHandler);

function myEventHandler(e){
    console.log(e.type); // Event type ('mousedown', 'mouseup' etc)
    console.log(this);  // The element that triggered the event.
    // Do stuff.
}

But you can't bind click to '#elementA' and mousedown to '#elementB' in 1 line. 但你不能绑定click'#elementA' mousedown'#elementB'在1号线。

So, no, there is no "One-liner" jQuery function to bind multiple different events to different elements. 因此,没有,没有将多个不同事件绑定到不同元素的“单线” jQuery函数。

Basically, Have a look at jQuery's .on() 基本上, 看看jQuery的.on()

You can use multiple jQuery selectors separated by commas (like CSS selectors). 您可以使用多个以逗号分隔的jQuery选择器(例如CSS选择器)。 The resulting jQuery object list will contain all of them. 生成的jQuery对象列表将包含所有这些对象。

Binding multiple events in jQuery is allowed if you use .bind() or .on() and set the first parameter to be a list of event names separated by spaces. 如果使用.bind().on()并将第一个参数设置为事件名称列表(用空格分隔.on() ,则允许在jQuery中绑定多个事件。

function handler_function (e) {
    e.target // contains the element that triggered the event (which was clicked for ex.)
    e.type // contains the event type that triggered the event
    e.keyCode // contains a number representing the key that has been pressed (if any)
}
$('button#button_id, input[type=text]#input_box')
    .bind('keyup click hover touch drag drop', handler_function);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM