简体   繁体   English

Jquery 与 ClipboardJS 最接近

[英]Jquery closest with ClipboardJS

I make a simple coupon clipper with ClipboardJS.我用 ClipboardJS 做了一个简单的优惠券剪。 My purpose is that when the user clicks on the Coupon button to clip it (copy).我的目的是当用户单击优惠券按钮时对其进行剪辑(复制)。 The Coupon will display the string Copied, then return to show the original Coupon after a moment. Coupon 将显示字符串 Copied,然后返回显示原始 Coupon。

Unfortunately, if there are two or more coupons on the same page, the returned value will always be the first Coupon.不幸的是,如果同一页面上有两个或多个优惠券,则返回的值将始终是第一个优惠券。 I tried to play around with jQuery closest, but I couldn't figure it out.我试图玩弄最接近的 jQuery,但我想不通。

Here is my code:这是我的代码:

HTML HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script>
<p>
Clip Coupon <button class="coupon_clipper" data-clipboard-text="10PERCENT">10PERCENT</button> to receive 10% discount for order over 100$
</p>
<p>
Clip Coupon <button class="coupon_clipper" data-clipboard-text="1PERCENT">1PERCENT</button> to receive 1% discount for order over 100$
</p>

<style>
.coupon_clipper {
  border-style: dashed;
}
</style>

Javascript Javascript

<script>var cClip = new ClipboardJS('.coupon_clipper');

cClip.on('success', function(e) {
  $(e.trigger).text('Clipped');
  e.clearSelection();
  setTimeout(function() {
    $(e.trigger).text($('.coupon_clipper').closest('.coupon_clipper').attr('data-clipboard-text'));
  }, 250);
});
</script>

JSFiddle: JSF中:

https://jsfiddle.net/whitecrown/tckd1820/3/ https://jsfiddle.net/whitecrown/tckd1820/3/

e.trigger return the element you clicked. e.trigger返回您单击的元素。 So you can use that to get the attribute as well as to set text.因此,您可以使用它来获取attribute以及设置文本。

Notice注意

let button = e.trigger;
setTimeout(function() {
  $(button).text($(button).attr('data-clipboard-text'));
}, 250);

 var cClip = new ClipboardJS('.coupon_clipper'); cClip.on('success', function(e) { let button = e.trigger; e.clearSelection(); $(button).text('Clipped'); setTimeout(function() { $(button).text($(button).attr('data-clipboard-text')); }, 250); });
 .coupon_clipper { border-style: dashed; }
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"></script> <p> Clip Coupon <button class="coupon_clipper" data-clipboard-text="10PERCENT">10PERCENT</button> to receive 10% discount for order over 100$ </p> <p> Clip Coupon <button class="coupon_clipper" data-clipboard-text="1PERCENT">1PERCENT</button> to receive 1% discount for order over 100$ </p>

$('.coupon_clipper').closest('.coupon_clipper') this will return always first, you have to think with respective to action you performed on which element. $('.coupon_clipper').closest('.coupon_clipper')这将始终首先返回,您必须考虑您对哪个元素执行的操作。 and in your case it will be the same so you can you directly e.trigger which returns the button you clicked.在你的情况下它会是一样的,所以你可以直接e.trigger返回你点击的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM