Jquery给指定元素绑定事件

news/2025/1/25 20:36:19

bind()

  • 注意:bind()函数在jquery1.7版本以前比较受推崇,1.7版本出来之后,官方已经不推荐用bind()

    :bind()函数只针对已经存在的元素进行事件的设置

  • 介绍:bind() 方法为被选元素添加一个或多个事件处理程序,并规定事件发生时运行的函数

  • 使用:绑定单个事件 模板: $(selector).bind(event,data,function)

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("button").bind("click",function(){$("p").slideToggle();});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>请点击这里</button>
</body>
</html>

绑定多个事件写法 : 模板: $(selector).bind({event:function, event:function, ...})

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("button").bind({click:function(){$("p").slideToggle();},mouseover:function(){$("body").css("background-color","red");},  mouseout:function(){$("body").css("background-color","#FFFFFF");}  });
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<button>请点击这里</button>
</body>
</html>

Unbind()

  • 介绍:unbind() 方法移除被选元素的事件处理程序。

    该方法能够移除所有的或被选的事件处理程序,或者当事件发生时终止指定函数的运行

  • 使用:去除p标签上所有事件 模板:$(selector).unbind(event,function)

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("p").click(function(){$(this).slideToggle();});$("button").click(function(){$("p").unbind();});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>这是另一个段落。</p>
<p>点击任何段落可以令其消失。包括本段落。</p>
<button>删除 p 元素的事件处理器</button>
</body>
</html>
​去除绑定的某一特定事件方式<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
function alertMe()
{
alert("Hello World!");
}
​
$(document).ready(function(){$("p").click(alertMe);$("button").click(function(){$("p").unbind("click",alertMe);});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>这是另一个段落。</p>
<p>点击任何段落可以触发一个提示框。包括本段落。</p>
<button>从 p 元素的 click 事件删除提示框函数</button>
</body>
</html>

live() on() delegate()使用方式基本与bind()并且都支持未来新添加元素的事件设置

以live()为例

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){$("p").live("click",function(){//此处的live()改成bind(),新增的<p>标签就没有此事件,而live()依然存在此事件$(this).slideToggle();});$("button").click(function(){$("<p>This is a new paragraph.</p>").insertAfter("button");});
});
</script>
</head>
<body>
<p>这是一个段落。</p>
<p>点击任意 p 元素会令其消失。包括本段落。</p>
<button>在本按钮后面插入新的 p 元素</button>
<p><b>注释:</b>通过使用 live() 方法而不是 bind() 方法,新的 p 元素同样会在点击时消失。</p>
</body>
</html>

现在更推荐使用on(),主讲on()

Reason

bind()函数在jquery1.7版本以前比较受推崇,1.7版本出来之后,官方已经不推荐用bind(), 替代函数为on(), 这也是1.7版本新添加的函数,同样,可以用来代替live()函数,live()函数在1.9版本已经删除; c、bind()、.live() 或.delegate(),查看源码就会发现,它们实际上调用的都是.on()方法

  1. 使用on()对类选择器进行单事件绑定

<body>2 <button id="add" type="button">add DIV</button>3 <button id="del" type="button">del DIV</button>4 <button id="onBtn" type="button">绑定事件</button>5 <button id="offBtn" type="button">解绑事件</button>6 <div id="container">7     <div class='created'>我是原生div<div/>8 </div>9 </body>
10 <script>
11     $(function () {
12         $("#add").click(function(){
13             $("#container").prepend("<div class='created'>我是动态生成的div<div/>")
14         });
15         $("#del").click(function(){
16             $("div").remove(".created:first")
17         });
18         $("#onBtn").click(function(){
19             $("#container").on("click",".created",function(){
20                 alert(1);
21             });
22         });
23         $("#offBtn").click(function(){
24             $("#container").off("click");
25         })
26     })
27 </script>

 


https://dhexx.cn/news/show-4151905.html

相关文章

用jquery让鼠标定位输入框

$(function(){$("#id").get(0).focus(); })

Spring 注解实现Bean依赖注入之@Qualifier

Qualifier&#xff1a;限定描述符&#xff0c;用于细粒度选择候选者&#xff1b; Autowired默认是根据类型进行注入的&#xff0c;因此如果有多个类型一样的Bean候选者&#xff0c;则需要限定其中一个候选者&#xff0c;否则将抛出异常 Qualifier限定描述符除了能根据名字进行…

freemarker获取list索引值

freemarker中遍历list获取索引值 <#list currentPathList as path><#if path_index 0></#if> </#list> as 后面的那个变量&#xff0c;加上_index&#xff0c;就可以表示当前循环到第几项

浅谈redis缓存及缓存雪崩的处理

目录 前言 代码分析 第一种代码案例&#xff1a; 第二种方案&#xff0c;加锁 第三种方案&#xff1a;semaphore实现共享锁 第四种方案&#xff1a;基于DCL&#xff08;Double Check Lock&#xff09;模式&#xff0c;结合Semaphore&#xff0c;再次进一步对代码进行优化。…

css控制div的子元素不换行

需求&#xff1a;如题&#xff1a; <div style"white-space:nowrap"><input typetext style"display:inline;width:180px;" ></input><button type"button" class"btn btn-default">确定</button></…

java代码获得文件的MD5

目录 什么是文件的MD5? java代码获得MD5的几种方式 方法一&#xff1a; 方法二&#xff1a; 方法三&#xff1a; 方法四&#xff1a; 方法五&#xff1a; 总结 什么是文件的MD5? MD5是文bai件签名&#xff0c;相当于我们的身份du证 独一无二的。MD5在论坛上、软件发布…

Redis的事务性

简介&#xff1a; Redis我们常常称其为内存数据库&#xff0c;而在传统的关系型数据库中&#xff0c;事务性又是不得不面临的一个问题&#xff0c;所谓事物性&#xff0c;说简单点&#xff0c;就是一组数据库操作之间是有关联关系的&#xff0c;要么全部都执行成功&#xff0c…

java 查询字符串中首个数字出现的位置

/*** 查询字符串中首个数字出现的位置* param str 查询的字符串* return 若存在&#xff0c;返回位置索引&#xff0c;否则返回-1&#xff1b;*/public static int findFirstIndexNumberOfStr(String str){int i -1;Matcher matcher Pattern.compile("[0-9]").matc…

Java 从List中删除空值

1. Java 7或更低版​​本&#xff1a;list.removeAll(Collections.singleton(null)); 2. Java 8或更高版本(推荐): public void removeAllNullsFromListWithJava8() {List<String> list new ArrayList<>(Arrays.asList("A", null, "B", nul…

nginx部署 上传文件提示413 Request Entity Too Large错误

现象&#xff1a; 在开发中&#xff0c;用nginx作为代理服务器进行web项目部署&#xff0c;在上传Excel文件时&#xff0c;出现如下错误&#xff1a; 原因&#xff1a; 这是因为nginx在默认的设置网页上传文件的最大值是1M client_max_body_size 1M #设置网页上传文件的最大…