`
jian0487
  • 浏览: 96209 次
  • 性别: Icon_minigender_1
  • 来自: 宁德
社区版块
存档分类
最新评论

jQuery:Events函数的使用

阅读更多
函数:blur(),click(),focus(),select(),submit()

功能:这类函数主要是引发对象的相应事件

返回:jQuery对象

例子:

jQuery Code

$("p").click();

Before

<p onclick="alert('Hello');">Hello</p>

Result:

alert('Hello');



jQuery Code

$("form").submit();


函数:blur(fn),click(fn),dblclick(fn),error(fn),focus(fn),

      keydown(fn),keypress(fn),keyup(fn),load(fn),

      mousedown(fn),mouseover(fn),mousemove(fn),mouseup(fn),

      ready(fn),resize(fn),scrool(fn),select(fn),unload(fn)

功能:给匹配元素添加相应的事件

返回:jQuery对象

参数:要激发的事件响应函数

例子:

jQuery Code

$("p").click( function() { alert("Hello"); } );

Before

<p>Hello</p>

Result:

<p onclick="alert('Hello');">Hello</p>


函数:bind(type, data, fn)

功能:给匹配元素绑定事件的响应函数

参数:type事件类型名,data一个用来个响应函数传递参数的json对象,fn响应函数

例子:

jQuery Code

$("p").bind("click", function(){

  alert( $(this).text() );

});

Before

<p>Hello</p>

Result:

alert("Hello")



Pass some additional data to the event handler.



jQuery Code

function handler(event) {

  alert(event.data.foo);

}

$("p").bind("click", {foo: "bar"}, handler)

Result:

alert("bar")


函数:unbind(type, fn)

功能:解除事件的绑定

返回:jQuery对象

参数:type事件的类型名,fn响应函数

例子:

jQuery Code

$("p").unbind()

Before

<p onclick="alert('Hello');">Hello</p>

Result:

[ <p>Hello</p> ]



jQuery Code

$("p").unbind( "click" )

Before

<p onclick="alert('Hello');">Hello</p>

Result:

[ <p>Hello</p> ]


函数:one(type, data, fn) 

功能:只响应一次事件,类似bind(type,data,fn)


函数:hover(over, out)

功能:设定鼠标划入和划出匹配元素的事件

返回:jQuery对象

参数:

over (Function): The function to fire whenever the mouse is moved over a matched element. 

out (Function): The function to fire whenever the mouse is moved off of a matched element. 

例子:

jQuery Code

$("p").hover(function(){

  $(this).addClass("hover");

},function(){

  $(this).removeClass("hover");

});


函数:trigger(type, data)

功能:引发相应事件

返回:jQuery对象

参数:type事件类型名,data要传递给事件处理函数的可选参数

例子:

jQuery Code

$("p").trigger("click")

Before

<p click="alert('hello')">Hello</p>

Result:

alert('hello')



Example of how to pass arbitrary data to an event



jQuery Code

$("p").click(function(event, a, b) {

  // when a normal click fires, a and b are undefined

  // for a trigger like below a refers too "foo" and b refers to "bar"

}).trigger("click", ["foo", "bar"]);



jQuery Code

$("p").bind("myEvent",function(event,message1,message2) {

    alert(message1 + ' ' + message2);

});

$("p").trigger("myEvent",["Hello","World"]);

Result:

alert('Hello World') // One for each paragraph


函数:toggle(even, odd)

功能:每次单击时切换事件处理

返回:jQuery对象

参数:

even (Function): The function to execute on every even click. 

odd (Function): The function to execute on every odd click. 

例子:

jQuery Code

$("p").toggle(function(){

  $(this).addClass("selected");

},function(){

  $(this).removeClass("selected");

});
分享到:
评论

相关推荐

    JQuery:常用方法一览

    - **说明**:使用此方法可以动态地更改页面元素的内容。 4. **`removeAttr(attribute)`**: - **功能**:删除指定的属性。 - **示例**:`$("element").removeAttr("class");` - **说明**:可以移除HTML元素的...

    jQuery:原始学习

    学习如何有效地使用jQuery,如避免不必要的DOM操作,使用 delegated events 减少事件监听器,以及利用`.on()`替代`.live()`等老方法。 10. **兼容性与现代前端技术**:了解jQuery如何与现代前端框架如React、Vue、...

    JQUERY技术内幕:深入解析QUERY架构设计与实现原理 完整版 共两个包

    进而通过“构造jquery对象”章节分析了构造函数jquery()的各种用法和内部构造过程;接着详细分析了底层支持模块的源码实现,包括:选择器sizzle、异步队列deferred、数据缓存data、队列queue、浏览器功能测试support...

    jquery实战:窗口效果

    2. **事件(Events)**:jQuery提供了一套丰富的事件处理方法,如`click()`, `mouseover()`, `mouseout()`等,用于响应用户交互。比如,当用户点击一个按钮,可以触发一个窗口弹出的效果。 3. **DOM操作(DOM ...

    jQuery:jQuery 源码分析

    2. **属性操作**:使用 `attr()` 和 `removeAttr()` 可以获取或删除元素的属性,而 `prop()` 用于处理布尔型属性,如 `checked` 或 `disabled`。 3. **内容操作**:`html()`, `text()`, `val()` 分别用于获取或设置...

    jqueryAPI jquery使用文档

    jQuery API 是开发者使用jQuery进行网页开发的重要参考资料,它提供了丰富的函数和方法,帮助我们高效地实现各种功能。 一、选择器(Selectors) jQuery 的核心功能之一就是选择器,它借鉴了CSS的选择器语法,使得...

    jquery API 使用手册

    《jQuery API 使用手册》是开发人员使用jQuery框架进行网页交互和功能实现的重要参考资料。jQuery是一个高效、简洁且易用的JavaScript库,它极大地简化了HTML文档遍历、事件处理、动画制作以及Ajax交互等任务。手册...

    jquery的fullcalendar使用案例

    在这个使用案例中,我们将深入探讨jQuery FullCalendar的基本使用、功能特性以及如何在实际项目中应用。 首先,jQuery FullCalendar的核心功能是展示和管理时间轴上的事件。它支持日、周、月视图,允许用户轻松切换...

    前端项目-jquery-outside-events.zip

    本项目“前端项目-jquery-outside-events”聚焦于一个特定的jQuery扩展,即`outside-events`,这是一个插件,它允许开发者绑定事件处理器到那些在指定元素外部触发的事件。 ### 一、jQuery Outside Events 插件 ...

    jquery三个版本使用手册chm.rar

    **jQuery 是一个广泛使用的 JavaScript 库,它极大地简化了网页中的 JavaScript 编程,使得DOM操作、事件处理、动画设计以及Ajax交互变得更加便捷。本文将详细介绍压缩包文件中包含的三个jQuery版本的手册,帮助您...

    前端项目-jquery-touch-events.zip

    通过使用这个jQuery Touch Events插件,开发者可以实现更流畅、无延迟的点击响应,从而提升用户在触摸设备上的交互体验。 在压缩包内的“jQuery-Touch-Events-master”目录中,我们可以预期找到源代码、示例、文档...

    jquery核心函数_.docx

    使用`$(element, attributes, events)`,你可以根据提供的HTML标记字符串动态创建DOM元素,并设置属性和事件。例如,`$(input, {type: 'button', value: '点击', click: function(){$(form)[0].submit();}})....

    JQuery1.4.1API.CHM以及对应函数库

    3. **事件处理(Events)**:jQuery的事件处理机制简化了JavaScript中的事件绑定,例如,`$("#element").click(function() {...})`会为指定元素添加点击事件监听器。此外,`.bind()`, `.unbind()`, `.delegate()`, `...

    jquery 最新版框架下载(1.32-1.8.3)

    •Event:使用委托事件和伪类时的性能衰退问题 •Misc:修复了一些兼容问题,并进行了优化 •Offset:在BlackBerry5和iOS3上使用会抛出一个错误 •修复了选择器的多个bug 详细信息:JQUERY 1.8.2 CHANGE LOG ...

    Jquery Calendar插件

    jQuery Calendar提供了一系列的事件回调函数,如`eventClick`(点击事件)、`dayClick`(单击日期)和`eventDrop`(拖放事件后的位置改变)。通过这些回调,你可以实现更复杂的业务逻辑,例如打开新的弹窗、发送Ajax...

    JQuery_Api文档

    3. **事件处理(Events)**:JQuery简化了事件绑定和触发。例如,`click(function() {...})`为元素绑定点击事件,`$(document).ready(function() {...})`确保在页面加载完成后执行指定的代码。 4. **动画(Effects...

    jQuery技术内幕 深入解析jQuery架构设计与实现原理

    进而通过“构造jquery对象”章节分析了构造函数jquery()的各种用法和内部构造过程;接着详细分析了底层支持模块的源码实现,包括:选择器sizzle、异步队列deferred、数据缓存data、队列queue、浏览器功能测试support...

    jquery-3.2.0.js,jquery-3.2.0.min.js

    - **绑定事件(Event Binding)**:使用 `.on()` 方法绑定事件,如 `.on('click', function() { ... })` 绑定点击事件。 - **事件委托(Event Delegation)**:使用 `.delegate()` 或 `.on()` 的事件委托功能处理...

    jquery-1.3.2.min.js

    - **jQuery与其他库的兼容**:jQuery设计时考虑了与其他库共存,如使用$.noConflict()避免命名冲突。 - **jQuery源码阅读**:理解.min.js背后的源码逻辑,有助于深入学习JavaScript和前端开发。 总之,jQuery ...

    jquery-events:事件处理

    在jQuery中,可以使用`stopPropagation()`阻止事件冒泡: ```javascript $('div').on('click', function(e) { e.stopPropagation(); // 其他代码 }); ``` 这样,点击`div`内的子元素时,事件不会传递到`div`元素。...

Global site tag (gtag.js) - Google Analytics