Примеры для jQuery .live()


Cancel a default action and prevent it from bubbling up by returning false.

1
2
3
$( "a" ).live( "click", function() {
return false;
});

Cancel only the default action by using the preventDefault method.

1
2
3
$( "a" ).live( "click", function( event ) {
event.preventDefault();
});

Bind custom events with .live().

1
2
3
4
5
6
7
8
9
10
11
12
$( "p" ).live( "myCustomEvent", function( event, myName, myValue ) {
$( this ).text( "Hi there!" );
$( "span" )
.stop()
.css( "opacity", 1 )
.text( "myName = " + myName )
.fadeIn( 30 )
.fadeOut( 1000 );
});
$( "button" ).click(function() {
$( "p" ).trigger( "myCustomEvent" );
});

Use an object to bind multiple live event handlers. Note that .live() calls the click, mouseover, and mouseout event handlers for all paragraphs--even new ones.

1
2
3
4
5
6
7
8
9
10
11
$( "p" ).live({
click: function() {
$( this ).after( "<p>Another paragraph!</p>" );
},
mouseover: function() {
$( this ).addClass( "over" );
},
mouseout: function() {
$( this ).removeClass( "over" );
}
});