zclip not working within bootstrap modal

2019-05-11 13:28发布

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?

2条回答
Summer. ? 凉城
2楼-- · 2019-05-11 14:23

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)

查看更多
何必那么认真
3楼-- · 2019-05-11 14:28

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();
    });
});
查看更多
登录 后发表回答