简体   繁体   English

如何隐藏我刚刚通过单击添加的 div?

[英]How can I hide a div that I've just added with a click?

I'm appending some HTML to my button on a click, like this:我在单击时将一些 HTML 附加到我的按钮,如下所示:

jQuery(document).ready(function($) {
    $('#sprout-view-grant-access-button').on('click', function(e) {
        $(this).toggleClass('request-help-cta-transition', 1000, 'easeOutSine');
        var callback = $(e.currentTarget).attr('data-grant-access-callback');
        var wrapper = $('.dynamic-container');
        console.log(wrapper);

        if( typeof window[callback] !== 'function') {
            console.log('Callback not exist: %s', callback);
        }

        var already_exists = wrapper.find('.main-grant-access');
        console.log(already_exists);

        if( already_exists.length ) {
            already_exists.remove();
        }

        var markup = $(window[callback](e.currentTarget));
        wrapper.append(markup);

    });
});

function generate_grant_access_container_markup() {
    var contact_data_array = contact_data;

    var template = jQuery('#template-sprout-grant-access-container')

    return mustache(template.html(), {
        test: 's'
    });
}

As per the code, whatever comes from generate_grant_access_container_markup will be put inside dynamic-container and shown.根据代码,来自generate_grant_access_container_markup任何内容都将放入dynamic-container并显示。

My problem is that, the newly added code just doesn't wanna dissapear upon clicking (toggle) of the button once again.我的问题是,新添加的代码只是不想在再次单击(切换)按钮时消失。

Here's my syntax / mustache template:这是我的语法/胡子模板:

<script type="template/mustache" id="template-sprout-grant-access-container">
    <p class="main-grant-access">{{{test}}}</p>
</script>

And here's the container:这是容器:

<div class="button-nice request-help-cta" id="sprout-view-grant-access-button" data-grant-access-callback="generate_grant_access_container_markup">
Grant Devs Access
    <div class="dynamic-container"></div>
</div>

I understand that the click event only knows about items that are in the DOM at the moment of the click, but how can I make it aware of everything that gets added after?我知道点击事件只知道点击时 DOM 中的项目,但我怎样才能让它知道之后添加的所有内容?

I would recommend visibility: hidden.我会推荐可见性:隐藏。 Both display none and removing elements from the dom mess with the flow of the website.两者都显示 none 和从 dom 中删除元素与网站的流程混乱。 You can be sure you would not affect the design with visibility: hidden.您可以确定您不会影响具有可见性的设计:隐藏。

I don't deal with Jquery at all but it seems like this Stack overflow covers the method to set it up well.我根本不处理 Jquery,但似乎这个堆栈溢出涵盖了设置它的方法。

Equivalent of jQuery .hide() to set visibility: hidden 等效于 jQuery .hide() 来设置可见性:隐藏

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

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