zclip not working within bootstrap modal

你。 提交于 2019-12-07 12:32:12

问题


I'm using the clean example code provided by zclip page:

$('a#copy-dynamic').zclip({
    path:'js/ZeroClipboard.swf',
    copy:function(){return $('input#dynamic').val();}
});

and this is the HTML:

<a href="#" id="copy-dynamic" class="">Click here to copy the value of this input:</a>
<input type="text" id="dynamic" value="Insert any text here." onfocus="if(this.value=='Insert any text here.'){this.value=''}" onblur="if(this.value==''){this.value='Insert any text here.'}">

It works fine if the HTML is inside the bootstrap main page, but it stops working if i move the html inside a bootstrap modal window (that is, inside the div element of the modal).

How can i get it work?


回答1:


I had the same issue with zclip and bootstrap modals. The fix I applied was twofold:

  • Attach the zclip to the element inside the modal's 'show' function.
  • Add a 250ms delay before attaching the zclip to the element

This properly places the zclip within the modal. It also works if you have multiple tabs in the modal.

HTML

<div id="myModal" class="modal hide fade">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
        <h3>Modal header</h3>
    </div>
    <div class="modal-body">
        <a class="btn" href="#" id="modal_body_button">Copy Undo Config To Clipboard</a>
        <p>One fine body…</p>
    </div>
    <div class="modal-footer">
        <a href="#" class="btn">Close</a>
        <a href="#" class="btn btn-primary">Save changes</a>
    </div>
</div>

JavaScript

$('#myModal').on('show', function () {
    $("#modal_body_button").delay(250).queue(function(next){
        $(this).zclip({
            path: "/static/javascript/ZeroClipboard.swf",
            copy: "copied text OK!"
        });
        next();
    });
});



回答2:


In example above can also use on('shown') instead of on('show') event which calls when modal completely showed. This helps to prevent using dirty hacks like delay(250)



来源:https://stackoverflow.com/questions/14281373/zclip-not-working-within-bootstrap-modal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!