//获取链接
$('.order-list').on('click', '.ajax_get_href', function () {
var goodsId = $(this).attr('data-goods_id');
console.log(goodsId);
var loadindex = layer.load();
var that = this;
$.ajaxSettings.async = false;
$.post('{:url("get_goods_url")}',{goods_id:goodsId},function(res){
console.log(res);
goods_url = res.info
$(that).attr('data-clipboard-text',goods_url);
layer.close(loadindex);
if(res.status == 0){
layer.msg(res.info,{icon:5,time:1500});
return false;
}else{
}
},'json');
$.ajaxSettings.async = true;
var clipboard = new Clipboard('.ajax_get_href');
clipboard.on('success', function(e) {
layer.msg('复制成功',{time:2000})
});
});
解析:通过按钮的data-goods_id参数,请求后端获取完整链接,但是异步操作最后clipboard总是为空,设置为同步操作即可。直接上代码