jquery:锚点类点击多次

时间:2014-03-28 06:29:00

标签: javascript jquery html onclick

我是jquery的新手。我希望在锚点击时克隆带有新id的div。但是当我点击锚元素时,div会被多次克隆。

代码:

$(document).on("click", "a.cls-copy", function(event){
               event.stopPropagation();
               div_id = $(this).closest('div').attr('id');
               var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
               console.log("newdiv : "+$(newdiv).attr('id'));
               if($(newdiv).find(".questiondata").length != 0)
               {
               $(newdiv).find(".questiondata").val("");
               }

               e_id = "edit"+countPage+"-"+questionCount;
               var temp_id= $(newdiv).attr('id');

               d_id = "del"+countPage+"-"+questionCount;
               c_id = "copy"+countPage+"-"+questionCount;
               questionCount++;

               showSuccessToast("Your question is copied");
               $("#"+temp_id).find(".cls-edit").attr('id',e_id);
               $("#"+temp_id).find(".cls-delete").attr('id',d_id);
               $("#"+temp_id).find(".cls-copy").attr('id',c_id);

               });

HTML

html+='<li><a data-role="button" class="km-widget km-button cls-delete" type="button" id="'+del_id+'" ><span class="km-text">delete</span></a>&nbsp;&nbsp;<a data-role="button" class="km-widget km-button cls-copy" type="button" id="'+copy_id+'"><span class="km-text">copy</span></a>&nbsp;&nbsp;<a data-role="button" class="km-widget km-button cls-edit" type="button" id="'+edit_id+'"><span class="km-text">edit</span></a></li>';

我哪里出错了?我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

找到解决方案:

 $(document).off("click","a.cls-copy").on("click", "a.cls-copy", function(event){
               event.stopPropagation();
               div_id = $(this).closest('div').attr('id');
               var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
               console.log("newdiv : "+$(newdiv).attr('id'));
               if($(newdiv).find(".questiondata").length != 0)
               {
               $(newdiv).find(".questiondata").val("");
               }

               e_id = "edit"+countPage+"-"+questionCount;
               var temp_id= $(newdiv).attr('id');

               d_id = "del"+countPage+"-"+questionCount;
               c_id = "copy"+countPage+"-"+questionCount;
               questionCount++;

               showSuccessToast("Your question is copied");
               $("#"+temp_id).find(".cls-edit").attr('id',e_id);
               $("#"+temp_id).find(".cls-delete").attr('id',d_id);
               $("#"+temp_id).find(".cls-copy").attr('id',c_id);

               });

使用此代码,

1).on - 您可以向DOM添加元素并仍处理click事件。

2).off - 删除事件处理程序

参考http://www.gajotres.net/prevent-jquery-multiple-event-triggering/

答案 1 :(得分:0)

   $(document).ready(function(){

        $("a.cls-copy").unbind('click').bind("click", function(event){
           alert("test");
           event.stopPropagation();
           div_id = $(this).closest('div').attr('id');
           var newdiv = $("#"+div_id).clone(true).attr('id',"newQuestionsDiv-Page"+countPage+"-"+questionCount).insertAfter("#"+div_id);
           console.log("newdiv : "+$(newdiv).attr('id'));
           if($(newdiv).find(".questiondata").length != 0)
           {
           $(newdiv).find(".questiondata").val("");
           }

           e_id = "edit"+countPage+"-"+questionCount;
           var temp_id= $(newdiv).attr('id');

           d_id = "del"+countPage+"-"+questionCount;
           c_id = "copy"+countPage+"-"+questionCount;
           questionCount++;

           showSuccessToast("Your question is copied");
           $("#"+temp_id).find(".cls-edit").attr('id',e_id);
           $("#"+temp_id).find(".cls-delete").attr('id',d_id);
           $("#"+temp_id).find(".cls-copy").attr('id',c_id);

           });
     });
  Replace your code with above code and check how much time test alerts ..