event.target in jquery
I have a table and I want to : for each click on item in t开发者_StackOverflow中文版able, this item will be added to another table. This is my code:
$("#listResult table td").click(function(e){
$("#addGroupMemberOfAdd").click(function(){
alert("Add to List Member");
$("#listMember table").append(
'<tr><td style="padding-left: 5px;">'+$(e.target).attr('rel')+'</td></tr>');
return false;
})
return false;
})
But that encounted a problem: if one table has 3 items, I click on item1, item1 is added to the other table, but when I click on item2, item1 is added to the other table again and item2 is added to this table. How to fix it????
Just a guess: when the second event fires, the event object (and thus it's target) may have changed.
(The way I read the code: when a td is clicked, a second click (e.g. on a OK button) moves it's content. Or does this second event fire immediately?)
I would suggest a different solution: The first click sets a variable to indicate the "active" td, the second click accesses this variable to see which td to move/add. In this way, you would avoid these anonymous functions (closures) generated on each click of td.
精彩评论