顯示具有 event 標籤的文章。 顯示所有文章
顯示具有 event 標籤的文章。 顯示所有文章

2008年8月19日 星期二

javascript event this

$('td.goal_del').bind('click', function(){goal_del(null,this );});
function goal_del(del_id,obj) {

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');
});
});

2008年8月14日 星期四

Working with Events, part 1

http://www.learningjquery.com/2008/03/working-with-events-part-1
http://manalang.com/jquery/docs/index.html#insertAfter-expr
http://manalang.com/jquery/docs/index.html#parents
http://www.quirksmode.org/js/events_order.html

Event Delegation. With event delegation, we bind the event handler to a containing element that remains in the DOM and then check for the target of the event.

$(document).ready(function() {
$('#list1 li.special button').click(function() {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
$(this).parent().after($newLi);
});
});

$(document).ready(function() {
$('#list2').click(function(event) {
var $newLi = $('<li class="special">special and new <button>I am new</button></li>');
var $tgt = $(event.target);
if ($tgt.is('button'))
$tgt.parent().after($newLi);
}

// next 2 lines show that you've clicked on the ul
var bgc = $(this).css('backgroundColor');
$(this).css({backgroundColor: bgc == '#ffcccc' || bgc == 'rgb(255, 204, 204)' ? '#ccccff' : '#ffcccc'});
});
});

parent(), after(), $(evnet.target), $(this), css()

可以用evnet.target 來取得被trigger的target, $(this)是整個$('#list2')