简体   繁体   English

切换隐藏/显示Div无效。

[英]Toggling hide/show Div's is not working.

I have a container (#main_Container) that holds multiple forms. 我有一个容纳多种形式的容器(#main_Container)。 Each form is within it's own container( .module ). 每个表单都在其自己的容器(.module)内。 Within each of the (.module) containers, I have 2 icons which need to be able to expand and collapse their form. 在每个(.module)容器中,我都有2个图标,它们需要能够展开和折叠它们的形式。 However, I cannot get them to act on their parent container. 但是,我无法让他们对他们的父容器采取行动。 Every time I click expand or contract, all forms are affected. 每次单击展开或收缩,所有表格都会受到影响。

<script>
    $(document).ajaxSuccess(function () {
        $("form.common").hide();

        $(".expand").click(function(){
            $(this).closest("form.common").show();
        });

        $(".collapse").click(function () {
            $(this).closest("form.common").hide();
        });
    });
</script>

<div id="main_Container">

    <div class="module">
        <h3>Customer Activity Log</h3>
        <div class="module_actions">
            <span class="icons_small right expand">+</span> <!-- -->
            <span class="icons_small right collapse">-</span> <!-- -->
        </div>
        <form class="common">
            <p>Form Stuff</p>
        </form>
    </div>

    <div class="module">
        <h3>Customer Activity Log</h3>
        <div class="module_actions">
            <span class="icons_small right expand">+</span> <!-- -->
            <span class="icons_small right collapse">-</span> <!-- -->
        </div>
        <form class="common">
            <p>Form Stuff</p>
        </form>
    </div>

</div>

.closest() will search for an element in the set, get the first element that matches the selector by testing the element itself and traversing up through its ancestors in the DOM tree. .closest()将在集合中搜索一个元素,通过测试该元素本身并遍历DOM树中的祖先来获得与选择器匹配的第一个元素。

According to your DOM structure, form.common is sibling element for .expand and .collapse parent. 根据你的DOM结构, form.common是同辈元素.expand.collapse父。

Try: 尝试:

$(document).ajaxSuccess(function () {
     $("form.common").hide();
     $(".expand").click(function(){
         $(this).parents().next("form.common").show();
     })
     $(".collapse").click(function() {
         $(this).parents().next("form.common").hide();
     });
 });

WORKING DEMO 工作演示

You are using .closest("form.common") , which only looks at the selected element and its parents up the DOM hierarchy. 您正在使用.closest("form.common") ,它仅查看选定元素及其在DOM层次结构中的元素。 Since the <form> tag is not a parent of the span, it will not be selected. 由于<form>标记不是跨度的父项,因此将不会选择它。

You can use something like: 您可以使用类似:

$(this).closest(".module_actions").next("form.common").show()

To work with expand/collapse, it'd be really helpful if you use IDs. 要使用展开/折叠,如果您使用ID,那将非常有帮助。 A solution would be add an id to each form and work with data attributes. 一个解决方案是在每个表单中添加一个id并使用数据属性。 It'd be something like this: 就像这样:

HTML HTML

<div class="module">
<h3>Customer Activity Log</h3>
<div class="module_actions">
   <span class="icons_small right expand" data-target="#form1">+</span> <!-- -->
   <span class="icons_small right collapse" data-target="#form1">-</span> <!-- -->
</div>
<form class="common" id="form1">
<p>Form Stuff</p>
</form>

</div>

JS JS

 $(".expand").click(function(evt){
   evt.preventDefault();

   var form = $(this).data("target");
   $(form).show();
});

$(".collapse").click(function(evt){
   evt.preventDefault();

   var form = $(this).data("target");
   $(form).hide();
});

Fiddle 小提琴

If editing the HTML is not an option, then @Unknown answer is the best way to handle it. 如果不能编辑HTML,则@Unknown答案是处理它的最佳方法。

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

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