简体   繁体   English

点击功能仅在第二次点击时触发

[英]On click function fires only on second click

I find few answers on this theme, but none of them helped me. 我没有找到关于该主题的答案,但没有一个对我有帮助。

My on click fires on second click - where is the problem? 我的点击点击触发第二次点击-问题出在哪里?

<script type="text/javascript">
$(function () {
   $("#back-in-stock-subscribe").click(function () {
      var sku = document.getElementById('back-in-stock-sku-to-get').textContent;
      var action = '@Url.RouteUrl("BackInStockSubscribePopup", new { productSku = "sku" })';
      action = action.replace("sku", sku);

      $("#back-in-stock-subscribe").fancybox({
           'href': action,
           'transitionIn': 'none',
           'width': 500,
           'height': 200,
           'type': 'iframe',
           'centerOnScroll': true
      });
   });
});
</script>

I believe your code on click initializes the fancy box but you need to call the click event on the fancybox itself to launch it. 我相信您的点击代码会初始化精美框,但您需要在精美框本身上调用click事件以启动它。

Try adding .click() at the end: 尝试在末尾添加.click():

<script type="text/javascript">
$(function () {
   $("#back-in-stock-subscribe").click(function () {
      var sku = document.getElementById('back-in-stock-sku-to-get').textContent;
      var action = '@Url.RouteUrl("BackInStockSubscribePopup", new { productSku = "sku" })';
      action = action.replace("sku", sku);

      $("#back-in-stock-subscribe").fancybox({
           'href': action,
           'transitionIn': 'none',
           'width': 500,
           'height': 200,
           'type': 'iframe',
           'centerOnScroll': true
      }).click();
   });
});
</script>

Looking at this : 看这个:

Open fancybox from function 从功能打开fancybox

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

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