在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問(wèn)答/網(wǎng)絡(luò)安全  HTML/ clipboard如何進(jìn)行異步的復(fù)制,即點(diǎn)擊復(fù)制按鈕時(shí)需要復(fù)制的內(nèi)容不存在

clipboard如何進(jìn)行異步的復(fù)制,即點(diǎn)擊復(fù)制按鈕時(shí)需要復(fù)制的內(nèi)容不存在

<span class="main-lottery--share">立即邀請(qǐng)</span>

$('.main-lottery--share').click(function(){

$.ajax({

    type: 'post',

    url: "<{$share_url}>",

    success:function(msg) {

        $(".main-lottery--share").attr("data-clipboard-text",msg);

    }

});

clipboard.on('success', function(e) {

    console.info(e);
    console.info('Action:', e.action);
    console.info('Text:', e.text);
    console.info('Trigger:', e.trigger);
    e.clearSelection();
    alert(e.text)

});

clipboard.on('error', function(e) {

    console.error('Action:', e.action);
    console.error('Trigger:', e.trigger);
    prompt("復(fù)制失敗,請(qǐng)手動(dòng)復(fù)制:",e.text)

});

點(diǎn)擊時(shí)發(fā)生ajax,之后才能獲得需要復(fù)制的內(nèi)容,填充點(diǎn)擊按鈕的data-clipboard-text屬性。所以第一次點(diǎn)擊無(wú)法復(fù)制所需要的內(nèi)容,有什么異步點(diǎn)擊復(fù)制的解決方法嗎?

回答
編輯回答
淺時(shí)光

可以頁(yè)面加載的時(shí)候先ajax請(qǐng)求,別點(diǎn)擊的時(shí)候請(qǐng)求

var clipboard = new Clipboard('.main-lottery--share');
//ajax請(qǐng)求
setTimeout(function () {
    $(".main-lottery--share").attr("data-clipboard-text", "aaaaaaaaaaa");
}, 0)

$('.main-lottery--share').click(function () {
    clipboard.on('success', function (e) {
        console.info(e);
        console.info('Action:', e.action);
        console.info('Text:', e.text);
        console.info('Trigger:', e.trigger);
        alert(e.text)
    });
});
2018年5月29日 13:08