2011-09-19 50 views
2

我有一个名单上的一个按钮,触发此事件:使追加发生仅在第一次

JS:

$(this).parents('tr') 
     .find('.class') 
     .append("<input type='text' size='5'/>"); 

它可能使这一追加在第一时间刚刚发生的呢?

像一个return false,不知道。

谢谢!

+0

我看到了这个以前的帖子:http://stackoverflow.com/questions/4115951/jquery-run-only-once那是你在找什么? – MRR0GERS

回答

6

假设此行为发生在click事件上,可以使用one。例如,

$('#my-button').one('click', function(e) { 
    e.preventDefault(); 
    $(this).parents('tr') 
     .find('.class') 
     .append("<input type='text' size='5'/>"); 
}); 


编辑:使用 delegate

var selector = '#my-button', 
    handler = function(e) { 
     // prevent default behavior 
     e.preventDefault(); 
     // make sure this only runs once 
     $(document).undelegate(selector, 'click', handler); 
     // actual behavior 
     $(this).parents('tr') 
      .find('.class') 
      .append("<input type='text' size='5'/>"); 
    }; 
$(document).delegate(selector, 'click', handler); 
+0

会试试这个,结果是brb。 –

+0

你能告诉我这与live()一起工作吗? –

+0

你能告诉我如何用live()完成这项工作吗? ** –