2008年8月15日 星期五

Working with Events, part 2

http://www.learningjquery.com/2008/05/working-with-events-part-2
http://www.learningjquery.com/wp-content/themes/jquery/docs.php?fn=clone
http://www.learningjquery.com/2007/09/namespace-your-events
http://docs.jquery.com/Core/jQuery.fn.extend


$('#list3 li.special button').click(function() {
var $parent = $(this).parent();
$parent.clone().insertAfter($parent);
});

$('#list4 li.special button').click(function() {
var $parent = $(this).parent();
$parent.clone(true).append(' I\'m a clone!').insertAfter($parent);
});

clone(true)


Event Namespacing


function addItemNS() {
$('#list7 li.special button')
.unbind('click.addit')
.bind('click.addit', function() {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
$(this).parent().after($newLi);
addItemNS();
});
}

$(document).ready(function() {
addItemNS();
// non-rebinding click handler
$('#list7 li.special button').click(function() {
$(this).after(' pressed');
});
});


Unbind by Function Reference

function addItemFinal() {
var $newLi = $('&lt;li class="special"&gt;special and new &lt;button&gt;I am new&lt;/button&gt;&lt;/li&gt;');
$(this).parent().after($newLi);
$('#list8 li.special button')
.unbind('click', addItemFinal)
.bind('click', addItemFinal);
}

$(document).ready(function() {

$('#list8 li.special button').bind('click', addItemFinal);
// non-rebinding click handler
$('#list8 li.special button').click(function() {
$(this).after(' pressed');
});
});

沒有留言: