📜  jQuery-事件处理

📅  最后修改于: 2020-10-23 08:17:59             🧑  作者: Mango


我们可以使用事件创建动态网页。事件是Web应用程序可以检测到的操作。

以下是示例事件-

  • 鼠标点击
  • 网页加载
  • 将鼠标移到元素上
  • 提交HTML表格
  • 键盘上的击键等

触发这些事件后,您便可以使用自定义函数对事件进行几乎所有的操作。这些自定义函数调用事件处理程序。

绑定事件处理程序

使用jQuery事件模型,我们可以使用bind()方法在DOM元素上建立事件处理程序,如下所示:

The jQuery Example
      
        
      
        
      
   
    
   
      

Click on any square below to see the result:

ONE
TWO
THREE

此代码将使除法元素响应click事件;此后,当用户在该部门内单击时,将显示警报。

这将产生以下结果-

bind()命令的完整语法如下:

selector.bind( eventType[, eventData], handler)

以下是参数的描述-

  • eventType-包含JavaScript事件类型的字符串,例如click或Submit。有关事件类型的完整列表,请参见下一部分。

  • eventData-这是可选参数,是将传递给事件处理程序的数据映射。

  • handler-每次事件触发执行的函数。

删除事件处理程序

通常,一旦建立了事件处理程序,该事件处理程序将在页面的剩余生命周期内保持有效。当您想删除事件处理程序时,可能会需要。

jQuery提供了unbind()命令来删除现有的事件处理程序。 unbind()的语法如下:

selector.unbind(eventType, handler)

or 

selector.unbind(eventType)

以下是参数的描述-

  • eventType-包含JavaScript事件类型的字符串,例如click或Submit。有关事件类型的完整列表,请参见下一部分。

  • handler-如果提供,则标识要删除的特定侦听器。

活动类型

Sr.No. Event Type & Description
1

blur

Occurs when the element loses focus.

2

change

Occurs when the element changes.

3

click

Occurs when a mouse click.

4

dblclick

Occurs when a mouse double-click.

5

error

Occurs when there is an error in loading or unloading etc.

6

focus

Occurs when the element gets focus.

7

keydown

Occurs when key is pressed.

8

keypress

Occurs when key is pressed and released.

9

keyup

Occurs when key is released.

10

load

Occurs when document is loaded.

11

mousedown

Occurs when mouse button is pressed.

12

mouseenter

Occurs when mouse enters in an element region.

13

mouseleave

Occurs when mouse leaves an element region.

14

mousemove

Occurs when mouse pointer moves.

15

mouseout

Occurs when mouse pointer moves out of an element.

16

mouseover

Occurs when mouse pointer moves over an element.

17

mouseup

Occurs when mouse button is released.

18

resize

Occurs when window is resized.

19

scroll

Occurs when window is scrolled.

20

select

Occurs when a text is selected.

21

submit

Occurs when form is submitted.

22

unload

Occurs when documents is unloaded.

事件对象

回调函数采用单个参数;调用处理程序时,JavaScript事件对象将通过它传递。

事件对象通常是不必要的,并且省略了参数,因为当绑定处理程序时,通常会提供足够的上下文,以便确切地知道触发处理程序时需要执行的操作,但是某些属性需要访问。

事件属性

Sr.No. Property & Description
1

altKey

Set to true if the Alt key was pressed when the event was triggered, false if not. The Alt key is labeled Option on most Mac keyboards.

2

ctrlKey

Set to true if the Ctrl key was pressed when the event was triggered, false if not.

3

data

The value, if any, passed as the second parameter to the bind() command when the handler was established.

4

keyCode

For keyup and keydown events, this returns the key that was pressed.

5

metaKey

Set to true if the Meta key was pressed when the event was triggered, false if not. The Meta key is the Ctrl key on PCs and the Command key on Macs.

6

pageX

For mouse events, specifies the horizontal coordinate of the event relative from the page origin.

7

pageY

For mouse events, specifies the vertical coordinate of the event relative from the page origin.

8

relatedTarget

For some mouse events, identifies the element that the cursor left or entered when the event was triggered.

9

screenX

For mouse events, specifies the horizontal coordinate of the event relative from the screen origin.

10

screenY

For mouse events, specifies the vertical coordinate of the event relative from the screen origin.

11

shiftKey

Set to true if the Shift key was pressed when the event was triggered, false if not.

12

target

Identifies the element for which the event was triggered.

13

timeStamp

The timestamp (in milliseconds) when the event was created.

14

type

For all events, specifies the type of event that was triggered (for example, click).

15

which

For keyboard events, specifies the numeric code for the key that caused the event, and for mouse events, specifies which button was pressed (1 for left, 2 for middle, 3 for right).

The jQuery Example
      
        
      
        
      
   
    
   
      

Click on any square below to see the result:

ONE
TWO
THREE

这将产生以下结果-

事件方法

有一个可以在事件对象上调用的方法列表-

Sr.No. Method & Description
1 preventDefault()

Prevents the browser from executing the default action.

2 isDefaultPrevented()

Returns whether event.preventDefault() was ever called on this event object.

3 stopPropagation()

Stops the bubbling of an event to parent elements, preventing any parent handlers from being notified of the event.

4 isPropagationStopped()

Returns whether event.stopPropagation() was ever called on this event object.

5 stopImmediatePropagation()

Stops the rest of the handlers from being executed.

6 isImmediatePropagationStopped()

Returns whether event.stopImmediatePropagation() was ever called on this event object.

事件处理方法

下表列出了重要的事件相关方法-

Sr.No. Method & Description
1 bind( type, [data], fn )

Binds a handler to one or more events (like click) for each matched element. Can also bind custom events.

2 off( events [, selector ] [, handler(eventObject) ] )

This does the opposite of live, it removes a bound live event.

3 hover( over, out )

Simulates hovering for example moving the mouse on, and off, an object.

4 on( events [, selector ] [, data ], handler )

Binds a handler to an event (like click) for all current − and future − matched element. Can also bind custom events.

5 one( type, [data], fn )

Binds a handler to one or more events to be executed once for each matched element.

6 ready( fn )

Binds a function to be executed whenever the DOM is ready to be traversed and manipulated.

7 trigger( event, [data] )

Trigger an event on every matched element.

8 triggerHandler( event, [data] )

Triggers all bound event handlers on an element.

9 unbind( [type], [fn] )

This does the opposite of bind, it removes bound events from each of the matched elements.

事件助手方法

jQuery还提供了一组事件帮助器函数,可用于触发事件以绑定上述任何事件类型。

触发方式

以下是将触发所有段落的模糊事件的示例-

$("p").blur();

装订方法

以下是将在所有

上绑定click事件的示例-

$("div").click( function () { 
   // do something here
});
Sr.No. Method & Description
1

blur( )

Triggers the blur event of each matched element.

2

blur( fn )

Bind a function to the blur event of each matched element.

3

change( )

Triggers the change event of each matched element.

4

change( fn )

Binds a function to the change event of each matched element.

5

click( )

Triggers the click event of each matched element.

6

click( fn )

Binds a function to the click event of each matched element.

7

dblclick( )

Triggers the dblclick event of each matched element.

8

dblclick( fn )

Binds a function to the dblclick event of each matched element.

9

error( )

Triggers the error event of each matched element.

10

error( fn )

Binds a function to the error event of each matched element.

11

focus( )

Triggers the focus event of each matched element.

12

focus( fn )

Binds a function to the focus event of each matched element.

13

keydown( )

Triggers the keydown event of each matched element.

14

keydown( fn )

Bind a function to the keydown event of each matched element.

15

keypress( )

Triggers the keypress event of each matched element.

16

keypress( fn )

Binds a function to the keypress event of each matched element.

17

keyup( )

Triggers the keyup event of each matched element.

18

keyup( fn )

Bind a function to the keyup event of each matched element.

19

load( fn )

Binds a function to the load event of each matched element.

20

mousedown( fn )

Binds a function to the mousedown event of each matched element.

21

mouseenter( fn )

Bind a function to the mouseenter event of each matched element.

22

mouseleave( fn )

Bind a function to the mouseleave event of each matched element.

23

mousemove( fn )

Bind a function to the mousemove event of each matched element.

24

mouseout( fn )

Bind a function to the mouseout event of each matched element.

25

mouseover( fn )

Bind a function to the mouseover event of each matched element.

26

mouseup( fn )

Bind a function to the mouseup event of each matched element.

27

resize( fn )

Bind a function to the resize event of each matched element.

28

scroll( fn )

Bind a function to the scroll event of each matched element.

29

select( )

Trigger the select event of each matched element.

30

select( fn )

Bind a function to the select event of each matched element.

31

submit( )

Trigger the submit event of each matched element.

32

submit( fn )

Bind a function to the submit event of each matched element.

33

unload( fn )

Binds a function to the unload event of each matched element.