开发者

Using zClip on click event of a jQuery UI Dialog button

开发者 https://www.devze.com 2023-03-19 11:50 出处:网络
I want to use a jQuery zClip plugin in jQuery UI dialog button, but I don\'t know how to adapt in this case. Anyone can h开发者_开发技巧elp me?

I want to use a jQuery zClip plugin in jQuery UI dialog button, but I don't know how to adapt in this case. Anyone can h开发者_开发技巧elp me?

Thank you in advance!

    $.ajax({
        url: '/music/lyrics/' + hash,
        success: function (data) {
            data = jQuery.parseJSON(data);
            $('#dialog-modal').html(data.lyrics);
            $('#dialog:ui-dialog').dialog('destroy');
            $('#dialog-modal').dialog({
                modal: true,
                resizable: false,
                title: 'Lyric: ' + data.song,
                width: 500,
                height: 400,
                buttons: {
                    'Copy' : function () {
                         // use zClip to copy $('#dialog-modal').text() here
                     }
                }
            });
        },
        error: function (msg) {
            alert(msg);
        }
    });


I would ignore the normal way dialog buttons handle actions, and separately use the way zClip handles actions. Something like this:

$.ajax({
        url: '/music/lyrics/' + hash,
        success: function (data) {
            data = jQuery.parseJSON(data);
            $('#dialog-modal').html(data.lyrics);
            $('#dialog:ui-dialog').dialog('destroy');
            $('#dialog-modal').dialog({
                modal: true,
                resizable: false,
                title: 'Lyric: ' + data.song,
                width: 500,
                height: 400,
                buttons: {
                    'Copy' : function () { return true; }
                }
            });
            $('#dialog-modal ui-button:contains(Copy)').zclip({
                path:'../whatever/ZeroClipboard.swf',
                copy:$('#dialog-modal').text()
            });
        },
        error: function (msg) {
            alert(msg);
        }
    });


Assuming you are using jQuery 1.8+, you can specifiy your buttons in a different way to add IDs to them:

$("#mydialog").dialog({
  ...
  buttons : [{
    text: "Close",
    click: function() {
      $(this).dialog("close");
    }
  },{
    text: "Copy to clipboard",
    id: "copyButton", // here is your ID
    click : function() {
      alert("Sorry, copy not supported in your browser, please copy manually.");
    }
  }]
  ...
});

//after .dialog("open");
$("#copyButton").zclip({
  ...
  clickAfter: false // dont propagate click: will suppress unsupported warning
  ...
});

The only issue I have is that it seem you can only mount zclip on visible buttons, so I do the zclip() call inside the handler for the button that opens the dialog

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号