首页 > 解决方案 > JQUERY Show overlay when button click work only first time. What's wrong?

问题描述

Why my overlay 'p' (bg purple with yellow text) is showing only once after button clicked, even if I have toggle the function?

Have a look to my codepen to test the issue:

https://codepen.io/cat999/project/editor/AEeEdg

    $('.animate-this').click(function() {
                $('p').slideToggle("fast");
        });

Easy way to fix this?

标签: javascriptjquerycss

解决方案


当可见时,该.animate-this元素被隐藏。<p>您可以将相同的功能添加到 中<p>,然后它将起作用。

$('.animate-this, p').click(function() {
    $('p').slideToggle("fast");
});

但是,我建议不要将事件投标给一个标签,而是一个类。因此,您只需将类添加.animate-this<p>标签中,它也可以工作,如下所示:

<p class="animate-this">If you click i need to show up again!</p>

推荐阅读