jQuery 事件方法大全-超全的总结

jQuery 提供了多种事件处理方法,包括 .on(), .off(), .trigger() 等。以下是一些常用的 jQuery 事件处理方法:

  1. .on(event, handler):绑定事件处理程序。



$("#button").on("click", function() {
    alert("按钮被点击了!");
});
  1. .off(event, handler):移除事件处理程序。



$("#button").off("click");
  1. .trigger(event, [eventData]):触发事件。



$("#button").trigger("click");
  1. .one(event, handler):绑定一次性事件处理程序。



$("#button").one("click", function() {
    alert("按钮只会被点击一次!");
});
  1. .bind(event, handler):(已废弃,请使用 .on())。
  2. .unbind(event, handler):(已废弃,请使用 .off())。
  3. .delegate(selector, event, handler):(已废弃,请使用 .on())。
  4. .undelegate(selector, event, handler):(已废弃,请使用 .off())。
  5. .hover([over], out):用于模拟鼠标悬停事件(鼠标指针移动到元素上面 & 移出元素)。



$("#button").hover(
    function() {
        // 鼠标指针移动到元素上面
        $(this).css("background-color", "yellow");
    }, 
    function() {
        // 鼠标指针移出元素
        $(this).css("background-color", "blue");
    }
);
  1. .focus(), .blur():处理焦点事件。



$("#inputField").focus(function() {
    $(this).css("background-color", "yellow");
});
 
$("#inputField").blur(function() {
    $(this).css("background-color", "white");
});
  1. .change():处理内容改变事件(如输入框内容改变)。



$("#inputField").change(function() {
    alert("输入字段的内容已改变!");
});
  1. .submit():处理表单提交事件。



$("#myForm").submit(function(event) {
    event.preventDefault(); // 阻止表单默认提交行为
    alert("表单将不会被提交!");
});
  1. .resize(), .scroll():处理窗口大小改变和滚动事件。



$(window).resize(function() {
    console.log("窗口大小已改变!");
});
 
$(window).scroll(function() {
    console.log("窗口滚动了!");
});
  1. .keydown(), .keyup(), .keypress():处理键盘按键事件。



$(document).keydown(function(event) {
    console.log("按下了键:" + event.key);
});
  1. .click(), .dblclick(), .mousedown(), .mouseup(), .mousemove(), .mouseenter(), .mouseleave():处理鼠标点击、双击、按下、抬起、移动、进入和离开事件。



$("#button").click(function() {
    alert("按钮被点击了!");
});
  1. .toggle(fn, fn):绑定
最后修改于:2024年08月22日 08:46

评论已关闭

推荐阅读

Vue中使用mind-map实现在线思维导图
2024年08月04日
VUE
Web前端最全Vue实现免密登录跳转的方式_vue怎么样不登录返回首页,最强技术实现
2024年08月04日
VUE
vue3 项目搭建教程(基于create-vue,vite,Vite + Vue)
2024年08月04日
VUE
Vue-颜色选择器实现方案——>Vue-Color( 实战*1+ Demo*7)
2024年08月04日
VUE
Vue项目卡顿慢加载?这些优化技巧告诉你!_vue数据多渲染卡顿
2024年08月04日
VUE
vue中的keep-alive详解与应用场景
2024年08月04日
VUE
Vue、React实现excel导出功能(三种实现方式保姆级讲解)
2024年08月04日
vue-office/docx插件实现docx文件预览
2024年08月04日
VUE
java调用js文件的两种方法(支持V8引擎)
2024年08月04日
JavaScript:解决计算精度问题/mathjs/bignumber.js/big.js/decimal.js
2024年08月04日
两周从爬虫小白变大神 _yjs_js_security_passport
2024年08月04日
JS笔记(对象、函数、数组)
2024年08月04日
Markdown.js:强大的纯JavaScript Markdown解析器
2024年08月04日
Vue项目:js模拟点击a标签下载文件并重命名,URL文件地址下载方法、请求接口下载文件方法总结。
2024年08月04日
vue 父组件怎么获取子组件里面的data数据
2024年08月04日
VUE
个人开发实现AI套壳网站快速搭建(Vue+elementUI+SpringBoot)
2024年08月04日
el-table 表格封装并改造实现单元格可编辑
2024年08月04日
none
nodejs环境下创建vue项目、SSH密钥登陆!!!
2024年08月04日
vue+quill+element-ui实现视频、图片上传及缩放保姆级教程,轻松使用富文本
2024年08月04日
【three.js】22. Imported Models导入模型
2024年08月04日