one(type,[data],fn)方法将处理程序绑定到一个或多个事件,该事件将针对每个匹配的元素执行一次。该处理程序仅对每个元素执行一次。否则,将应用与bind()中描述的相同规则。
selector.one( type, [data], fn )
这是此方法使用的所有参数的描述-
链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/events-one.html
来源:LearnFk无涯教程网
type - 事件类型。
data - 这是可选参数,表示作为event.data传递到事件处理程序的其他数据。
fn - 绑定到每个匹配元素上的事件的函数。
以下是一个简单的示例,简单说明了此方法的用法。 在这里,它将click事件与每个<div>元素绑定在一起。 尝试单击任意正方形两次,它不会像bind()方法那样反应-
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(document).ready(function() { $('div').one('click', function( event ){ alert('Hi there!'); }); }); </script> <style> .div{ margin:10px;padding:12px; border:2px solid #666; width:60px;} </style> </head> <body> <p>Click on any square below to see the result:</p> <div class="div" style="background-color:blue;"></div> <div class="div" style="background-color:green;"></div> <div class="div" style="background-color:red;"></div> </body> </html>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)