jquery_事件

// jquery_事件

// 代码1
/* HTML:
 *
 * <a id="test-link" href="#0">点我试试</a>
 *
 */
// 获取超链接的jQuery对象:
var a = $('#test-link');
a.on('click', function () {
    alert('Hello!');
});
//代码解说:on()方法用来绑定事件和函数

// 代码2
$(document).ready(function () {
    // init...
});
$(function () {
    // init...
});
//代码解说: ready()在dom完成初始化后触发,上面的代码是两种写法

// 代码3
$(function () {
    $('#testMouseMoveDiv').mousemove(function (e) {
        $('#testMouseMoveSpan').text('pageX = ' + e.pageX + ', pageY = ' + e.pageY);
    });
});
//代码解说:所有事件都会传入参数e,这样我们可以获得鼠标位置和按键的值

// 代码4
function hello() {
    alert('hello!');
}
a.click(hello); // 绑定事件
// 10秒钟后解除绑定:
setTimeout(function () {
    a.off('click', hello);
}, 10000);
//代码解说:off()用来解除事件与函数的绑定

  

猜你喜欢

转载自www.cnblogs.com/mexding/p/9060830.html