js常用事件总结
一.live(),click(),delegate(),bind(),change(),.on详解
参考:http://www.w3school.com.cn/jquery/jquery_ref_events.asp
1.live() 方法为被选元素附加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。
通过 live() 方法附加的事件处理程序适用于匹配选择器的当前及未来的元素(比如由脚本创建的新元素)。
语法:$(selector).bind(event,data,function),如果将live改成bind,未来添加的元素不具备该效果
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("p").live("click",function(){ $(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>
2.bind()规定向被选元素添加的一个或多个事件处理程序,以及当事件发生时运行的函数。
<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>
3.delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。
使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。
语法:$(selector).delegate(childSelector,event,data,function)
<html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $("div").delegate("p","click",function(){ $(this).slideToggle(); }); $("button").click(function(){ $("<p>这是一个新段落。</p>").insertAfter("button"); }); }); </script> </head> <body> <div style="background-color:yellow"> <p>这是一个段落。</p> <p>请点击任意一个 p 元素,它会消失。包括本段落。</p> <button>在本按钮后面插入一个新的 p 元素</button> </div> <p><b>注释:</b>通过使用 delegate() 方法,而不是 live(),只有 div 元素中的 p 元素会受到影响。</p> </body> </html>
二.href="#",href="javascript:;",href="javascript:void(0)"
原csdn博客地址:http://write.blog.csdn.net/postlist