简体   繁体   English

事件委托问题

[英]Issue with event delegation

I am trying to clone a table once the table has been populated. 表格填充后,我正在尝试克隆表格。 Inside the td I placed some input and textarea tags. 在td内,我放置了一些输入和textarea标签。 The problem that I have is that the contents inside the table are not cloned. 我的问题是表中的内容未克隆。 I have tried to use event delegation, but it seems that I am doing something wrong. 我曾尝试使用事件委托,但似乎我做错了事。 Here is the JSfiddle, write something inside the table and then press clone.`` http://jsfiddle.net/no84bror/2/ 这是JSfiddle,在表中写一些东西,然后按clone.`` http://jsfiddle.net/no84bror/2/

  $("#clonetable").on('click','textarea',function(){
    var tempTable = $('#masterTable');
    var temClone = $("<div/>").append(tempTable.clone()).html(); 
   // alert(temClone);
    var rep = temClone.replace("textarea","p");
    $("#a").html(rep);
    });

This is a jquery bug - deep cloning does not work for textareas http://bugs.jquery.com/ticket/3016 It started as a problem in Firefox but is the same in Chrome, apparently. 这是一个jquery错误-深度克隆不适用于textareas http://bugs.jquery.com/ticket/3016它最初在Firefox中是一个问题,但在Chrome中显然是相同的。

"The current behavior is documented at api.jquery.com and of course here. A plugin is available to provide the requested behavior. The ticket is marked patchwelcome, with the caveat that fixing this edge case inside jQuery causes a performance hit for the 90% of the time when it isn't needed." “当前行为已在api.jquery.com上进行了记录,当然也可以在此处进行记录。可以使用插件来提供所请求的行为。该票证被标记为patchwelcome,但需要注意的是,修复jQuery内部这种边缘情况会导致性能下降90不需要的时间百分比。”

The following code works and copies the contents of the input field, but due to the above bug you will have to copy the contents of textareas yourself or use the plugin. 以下代码可以工作并复制输入字段的内容,但是由于上述错误,您将不得不自己复制textareas的内容或使用插件。

$("#clonetable").on('click', function(){
     $("#a").html($('#masterTable').clone());
});

you can try using .clone( [withDataAndEvents][, deepWithDataAndEvents] ) , in other words using .clone(true, true) , but it makes no difference. 您可以尝试使用.clone( [withDataAndEvents][, deepWithDataAndEvents] ) ,换句话说,使用.clone(true, true) ,但这没什么区别。


Here's the code, including the hack to copy textarea content: 这是代码,包括复制textarea内容的技巧:

 $("#clonetable").on('click',function(){
    $("#a").html($('#masterTable').clone());
    var my_textareas     = $('#masterTable textarea').slice(2,4);
    var result_textareas = $("#a textarea");

    for (var i = 0, l = my_textareas.length; i < l; ++i){
        $(result_textareas[i]).val($(my_textareas[i]).val());
    }
 });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM