2015-02-14 2 views
0

Я не знаю, что я делаю неправильно в приведенном ниже коде. Когда я нажимаю на Копировать код, который я вставлял динамически, я получаю только последние значения viz (Текст 264). на всех кнопках. Не могли бы вы указать, где я ошибаюсь. Цените любую помощь.Несколько экземпляров клипа ZeroClipboard not workinig

<div id="keylist"> 
 
     <p>Copy the code and insert into your homepage to display</p> 
 

 
     <div class"row"> 
 

 
         
 
      <pre> Text 1 </pre> 
 
     </div> 
 
     <div class"row"> 
 

 
         
 
      <pre> Text 2 </pre> 
 
     </div> 
 
     <div class"row"> 
 

 
         
 
      <pre> Text 4 </pre> 
 
     </div> 
 
     <div class"row"> 
 

 
        
 
      <pre> Text 9 </pre> 
 
     </div> 
 
     <div class"row"> 
 

 
         
 
      <pre> Text 264 </pre> 
 
     </div> 
 

 
    
 
</div> 
 

 

 
<script language="JavaScript"> 
 
    $(document).ready(function() { 
 
     
 
     ZeroClipboard.setDefaults({ moviePath: '/Content/ZeroClipboard.swf' }); 
 
     var preNum = 1 
 

 
     $('pre').each(function() { 
 
      // Get a unique id for the element I will be inserting 
 
      var id = 'copy-btn-' + preNum++ 
 
      // Capture the text to be copied to the clipboard 
 
      var text = $(this).text() 
 
      
 
      // Insert the element, just before this 
 
      $('<div class="copy-btn" id="' + id + '-cont"><a class="icon-file icon-white" id="' + id + '">Copy Code</a></div>').insertBefore(this) 
 
      // Capture the newly inserted element 
 
      var elem = $(this).prev() 
 
      
 
      // Create the clip, and glue it to the element 
 
      var clip = new ZeroClipboard(); 
 

 

 
      clip.setText(text); 
 

 
      clip.glue(elem[0]); 
 
      
 
     }) 
 

 

 
    }); 
 
</script>

+0

Какую версию ZeroClipboard вы используете? –

+0

Я использую v1.3.0-beta.1 из ZeroClipBoard – Zubair

+0

Fabricio, я обновился до * v2.2.0, но все еще не работает – Zubair

ответ

1

ZeroClipboard 2.x изменил совсем немного API.

Использование ZeroClipboard 2.2.0 (не забудьте обновить ZeroClipboard в .js и .swf файлы):

ZeroClipboard.config({ swfPath: '/Content/ZeroClipboard.swf' }); 

$(document).ready(function() { 
    $('pre').each(function() { 
     // Get the text to be copied to the clipboard 
     var text = $(this).text(); 

     // Create the copy button 
     var $copyBtn = $('<div class="copy-btn"><a class="icon-file icon-white">Copy Code</a></div>') 
      .attr('data-clipboard-text', text) // set the text to be copied 
      .insertBefore(this); // insert copy button before <pre> 

     // Create the clip, and glue it to the copy button 
     new ZeroClipboard($copyBtn); 
    }); 
}); 

Я также очистил код немного, идентификаторы казалось ненужным.

Смежные вопросы