我正在尝试编写一个基本的博客平台,我希望为用户提供将前块内的代码复制到剪贴板的功能.
我使用ZeroClipboard来实现这一目标.文档准备好后,我遍历页面上的每个pre,向其添加一个剪贴板项,如下所示:
$(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) }) });
当我尝试这样做时,javascript控制台报告:Uncaught TypeError:无法读取未定义的属性’zIndex’
我目前对这个问题的理解是,当我试图将夹子粘到它上面时,插入的元件还没有在dom中可用,这就是为什么没有粘合的原因.
有谁知道我怎么能做到这一点?