在没有.click()的情况下启动leanModal

leanModal initiate without .click()

本文关键字:情况下 启动 leanModal click      更新时间:2023-09-26

我使用的是leanModal http://leanmodal.finelysliced.com.au需要启动它来打开一个div,但没有.click()方法。基本上,我想做这个。

  if(cartItems === 0){
    $("#cartEmpty").leanModal(); // #cartEmpty is my div with the message that needs to be initiated.
  } else {
    $("#nextStep").leanModal(); // #nextStep is my div is the form 
  }

对这个有什么想法吗?

我浏览了一下leanmodal的源代码,看起来你不能。你仍然需要一个链接来触发它。但是,您应该能够执行如下未经测试的代码

添加一些不可见的链接。内联样式是一件坏事,只做内联来简化

<a href="#cartEmpty" id="showCartEmpty" style="display:none" rel="leanModal" name="cartEmpty">empty cart</a>
<a href="#nextStep" id="showNextStep" style="display:none" rel="leanModal" name="nextStep">next step</a>

为leanmodal进行正常设置

$(function() {
    $('a[rel*=leanModal]').leanModal();     
});

在你的虚拟不可见链接

上调用click方法
  if(cartItems === 0){
    $("#showCartEmpty").click(); // in theory this'll cause the modal to be shown
  } else {
    $("#showNextStep").click(); // in theory this'll cause the modal to be shown
  }

如果做不到这一点,源代码是相当小的,你应该能够把它整理到你自己的项目中,并修改它,使其可以在要进行模态化的东西上调用,而不是启动模态的东西