jQuery事件

来源:互联网 发布:知乎人民通惠商城 编辑:程序博客网 时间:2024/06/06 00:57

1、ready()事件

在文档加载完成后激活函数;

$(document).ready(function(){  $(".btn1").click(function(){    $("p").slideToggle();  });});

2、keydown()方法

$('#password').keydown(function(){    if (event.keyCode ==13) {        $('#login').trigger('click');    };});

3、resize()方法

当调整浏览器窗口大小时,触发resize事件。

$(window).resize(function(){var width  = $(window).width() - $('#left').outerWidth() - $('.dl-second-slib-con').outerWidth();var height = $(window).height() - $('#head').outerHeight();$('#left').outerHeight(height);$('#right').outerHeight(height);$('#right').outerWidth(width);}).trigger('resize');
4、delegate()方法
<p style="margin-top: 12px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; border: 0px; line-height: 18px; font-family: Verdana, Arial, 宋体; background-color: rgb(249, 249, 249);">delegate() 方法为指定的元素(属于被选元素的子元素)添加一个或多个事件处理程序,并规定当这些事件发生时运行的函数。使用 delegate() 方法的事件处理程序适用于当前或未来的元素(比如由脚本创建的新元素)。</p>
<pre name="code" class="javascript">$('div').delegate('#audit','click',function(){alert($(this).val());})


5、on()方法

var audit = $('div').on('click','#audit',function(){console.log(123);})
on()方法和delegate()方法可以用于动态页面的点击事件,如果div是包含两层的,会执行两次事件,所以最好用类来缩小范围。如下:

$('.content-data').on('click','#audit',function(){console.log(123);})




0 0
原创粉丝点击