这个 绑定() 是jQuery中的一个内置方法,用于为所选元素附加一个或多个事件处理程序,该方法指定事件发生时要运行的函数。 语法:
null
$(selector).bind(event, data, function);
参数: 它接受下面指定的三个参数-
- 活动: 这是传递给选定元素的事件类型。
- 数据: 这是可以在所选元素上显示的数据。
- 功能: 这是由所选元件执行的功能。
返回值: 它返回对选定元素所做的所有修改。
< 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 > |
输出: 单击“单击我”框之前- 点击“点击我”框后-
代码#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 > |
输出: 单击“单击我”框之前- 点击“点击我”框后-
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END