2012-10-26 3 views
5

기본 블로깅 플랫폼을 작성하려고합니다. 사용자에게 미리 블록 내의 코드를 클립 보드에 복사 할 수있는 기능을 제공하고 싶습니다.새로 삽입 된 요소에 ZeroClipboard 클립을 어떻게 붙일 수 있습니까?

나는 이것을 달성하기 위해 ZeroClipboard을 사용하고 있습니다. 문서가 여기에 클립 보드 항목을 추가하는 페이지의 각 pre을 통해, I 루프, 준비되면 다음과 같이

$(document).ready(function() { 

     ZeroClipboard.setMoviePath('ZeroClipboard/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"><i class="icon-file icon-white" id="' + id + '"></i></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.Client(); 
      clip.setText(text) 
      clip.glue(elem) 
     }) 
    }); 

나는이 작업을 수행하려고하면 자바 스크립트 콘솔 보고서 : Uncaught TypeError: Cannot read property 'zIndex' of undefined

나의 현재를 문제의 이해는 클립을 접착제로 붙이려고 할 때 삽입 된 요소가 아직 dom에서 사용 가능하지 않기 때문에 접착제가 전혀 사용되지 않는 이유입니다.

아무도 내가 이것을 성취 할 수 있을지 모른다. Gluing instructions 가입일

답변

2

:

위와 같이 사용자는 DOM 요소 ID를 전달하거나 실제 DOM 요소 객체 자체에 대한 참조.

jQuery 객체를 전달하기 때문에 코드가 작동하지 않습니다.

clip.glue(id + '-cont') 

또는 실제 DOM 요소 참조 :

사용자는 ID를 전달할 수

clip.glue(elem[0]) 

상기 예에서는 .get()의 jQuery 객체 메소드에 대한 속기를 사용한다.

+1

그레이트 콜. 도와 주셔서 감사합니다. – finiteloop