简体   繁体   English

如何在JQuery / JS中触发data- *事件?

[英]How to trigger data-* event in JQuery/JS?

I'm actually new to JQuery, and i am trying to trigger a quick view from the css framework Bulma, you can get documentation on the quickview here: 我实际上是JQuery的新手,并且我试图从CSS框架Bulma触发快速视图,您可以在此处获取有关快速视图的文档:

https://wikiki.github.io/components/quickview/ https://wikiki.github.io/components/quickview/

The quickview is set like this: 快速视图的设置如下:

<div id="quickviewDefault" class="quickview">
  <header class="quickview-header">
    <p class="title">Quickview title</p>
    <span class="delete" data-dismiss="quickview"></span>
  </header>

  <div class="quickview-body">
    <div class="quickview-block">
      ...
    </div>
  </div>

  <footer class="quickview-footer">

  </footer>
</div>

And i can call it with this button (as saw on the wiki): 我可以使用此按钮调用它(如Wiki所示):

<button class="button is-primary" data-show="quickview" data-target="quickviewDefault">Show quickview</button>

So far its working, but i want to create a second button that ll call the quickview with JQuery, how can i do it ? 到目前为止,它的工作正常,但是我想创建第二个按钮,该按钮将使用JQuery调用quickview,我该怎么做?

So far i've tried to click on the button who work great with JQuery when i click on my second button. 到目前为止,当我单击第二个按钮时,我尝试单击与JQuery配合使用的按钮。

<!-- First button, work great -->
<button class="button is-primary" data-show="quickview" data-target="quickviewDefault">Show quickview</button>

<!-- Second button, doesn't work yet -->
<button class="clickmeplz">></button>

The first button is working well, and the second one should call the same quickview as the first one but with JQuery. 第一个按钮运行良好,第二个按钮应该使用第一个按钮调用与JQuery相同的快速视图。

This is the code i've tried so far with my second button. 这是我到目前为止使用第二个按钮尝试过的代码。

$(document).ready(function(){
    $(".clickmeplz").click(function(){
         $('button[data-target="quickviewDefault"]').click();
    });
});

Unfortunately, it seem that the method click() cannot trigger the event of my first button like this. 不幸的是,方法click()似乎无法像这样触发我的第一个按钮的事件。

Hello ! 你好 !

Let's try this: 让我们尝试一下:

$(document).ready(function(){
    $(".clickmeplz").click(function(){
        if($('div#quickviewDefault.is-active').length) { //If the quickview is showed
            $('div#quickviewDefault.is-active').removeClass('is-active'); //Remove the class to hide
        }else { 
            $('div#quickviewDefault').addClass('is-active'); //Add the class to show
        }
    });
});

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

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