jQuery | bind()及其示例

这个 绑定() 是jQuery中的一个内置方法,用于为所选元素附加一个或多个事件处理程序,该方法指定事件发生时要运行的函数。 语法:

null
$(selector).bind(event, data, function);

参数: 它接受下面指定的三个参数-

  1. 活动: 这是传递给选定元素的事件类型。
  2. 数据: 这是可以在所选元素上显示的数据。
  3. 功能: 这是由所选元件执行的功能。

返回值: 它返回对选定元素所做的所有修改。

显示bind()方法工作的jQuery代码:
代码#1:

< html >
< head >
jquery/3.3.1/jquery.min.js"></ script >
< script >
$(document).ready(function() {
$("p").bind("click", function() {
alert("Given paragraph was clicked.");
});
});
</ script >
< style >
p {
display: block;
padding: 50px;
width: 280px;
border: 2px solid green;
}
</ style >
</ head >
< body >
< p >Click me!</ p >
</ body >
</ html >


输出: 单击“单击我”框之前- 图片[1]-jQuery | bind()及其示例-yiteyi-C++库 点击“点击我”框后- 图片[2]-jQuery | bind()及其示例-yiteyi-C++库

代码#2: 在下面的代码中,添加了相同的“click”事件,但这次数据也传递给了这个方法。

< html >
< head >
jquery/3.3.1/jquery.min.js"></ script >
< script >
function handlerName(e) {
alert(e.data.msg);
}
<!--Here data is passing along with a function in
bind method-->
$(document).ready(function() {
$("p").bind("click", {
msg: "You just clicked the paragraph!"
}, handlerName)
});
</ script >
< style >
p {
display: block;
padding: 50px;
width: 280px;
border: 2px solid green;
}
</ style >
</ head >
< body >
<!--A pop will appear after clicking on this paragraph-->
< p >Click me!</ p >
</ body >
</ html >


输出: 单击“单击我”框之前- 图片[3]-jQuery | bind()及其示例-yiteyi-C++库 点击“点击我”框后- 图片[4]-jQuery | bind()及其示例-yiteyi-C++库

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享