我爱编程

jquery DOM&事件

2016-08-10  本文已影响71人  coolheadedY

问答

bind: function( types, data, fn ) {
    return this.on( types, null, data, fn );
}
unbind: function( types, fn ) {
    return this.off( types, null, fn );
}
live: function( types, data, fn ) {
    jQuery( this.context ).on( types, this.selector, data, fn );
    return this;
}
die: function( types, fn ) {
    jQuery( this.context ).off( types, this.selector || "**", fn );
    return this;
}
delegate: function( selector, types, data, fn ) {
    return this.on( types, selector, data, fn );
}
undelegate: function( selector, types, fn ) {
    return arguments.length === 1 ? this.off( selector, "**" ) : this.off( types, selector || "**", fn );
}

通过on()实现其他绑定:

//bind
$( "#id" ).bind( "click", function( e ) {} );
$( "#id" ).on( "click", function( e ) {} ); 
// delegate
$( "#id" ).delegate( "a", "click", function( e ) {} );
$( "#id" ).on( "click", "a", function( e ) {} );
*  unbind:从元素上删除一个以前附加事件处理程序。每个用.bind()

方法绑定的事件处理程序可以使用.unbind()
移除。

$('#id').unbind();
$('#id').unbind('click');//更加精确
$("#id").unbind( "click", handler );//指定阻止事件绑定的函数
var timesClicked = 0;
$( "#id" ).bind( "click", function( event ) {
alert( "The quick brown fox jumps over the lazy dog." );
timesClicked++;
if ( timesClicked >= 3 ) {
$( this ).unbind( event );
}
});//从自身内部处理程序时解除
*  off():移除一个事件处理函数。off()

方法移除用.on()
绑定的事件处理程序。

$("p").off()//移除所有段落上的事件
$("p").off( "click", "**" )//移除所有段落上的click事件
$("body").on("click", "p", func);
$("body").off("click", "p", func);//通过传入的第三个参数,仅移除先前绑定的事件处理函数
show();//直接显示
show(speed);//第一个参数显示所花费的时间
show(speed, function);//第二个参数显示完成后执行的函数
hide();//直接隐藏
hide(speed);//第一个参数隐藏所花费的时间
hide(speed, function);//第二个参数隐藏完成后执行的函数
$("#hidr").click(function () {
        $("span:last-child").hide("fast", function () {
          $(this).prev().hide("fast", arguments.callee);
});//当点击后选择最后一个元素进行隐藏,并使用递归和arguments.callee继续执行这个函数使隐藏元素的上一个元素也进行隐藏
$('li').animate({
opacity: .5,
height: '50%'
},
{
step: function() {
//todo
}
});

动画--参考
动画的完成finish()和停止stop()--参考

代码

var products = [
    {
        img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
        name: '珂兰 黄金手 猴哥款',
        price: '¥405.00'
    },{
        img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
        name: '珂兰 黄金转运珠 猴哥款',
        price: '¥100.00'
    },{
        img: 'http://img10.360buyimg.com/N3/jfs/t2242/92/1446546284/374195/9196ac66/56af0958N1a723458.jpg',
        name: '珂兰 黄金手链 3D猴哥款',
        price: '¥45.00'
    }
];

本博客版权归 本人和饥人谷所有,转载需说明来源

上一篇下一篇

猜你喜欢

热点阅读