简体   繁体   English

加载时自动切换展开/折叠箭头

[英]Auto toggle expand/collapse arrow on load

I have created a vertical list menu which consist of certain menu items. 我创建了一个垂直列表菜单,其中包含某些菜单项。 On clicking any menu item, the list expands to show its inner sub menu items. 单击任何菜单项后,列表将展开以显示其内部子菜单项。

Also, when the page is loaded, one of the menu will expand automatically according to the page. 同样,加载页面时,菜单之一将根据页面自动展开。 I used jquery toggle for this and it is working fine. 我为此使用了jQuery切换,并且工作正常。

Now I have placed collapse/expand arrow with this menu similar to those in jquery accordian and they toggle on click event. 现在,我在此菜单中放置了折叠/展开箭头,类似于在jQuery Accordian中的菜单,它们在单击事件上切换。

My problem is that when page loads, one of the menu items collapse but the arrow does not collapse with it and when I click on that menu again to collapse/toggle back, the arrow starts to toggle. 我的问题是,当页面加载时,菜单项之一会折叠,但是箭头不会随之折叠,而当我再次单击该菜单以折叠/切换回时,箭头会开始切换。

The situation can be seen as, On loading window - 情况可以看成是,在加载窗口上-

►Menu1 ►菜单1

submenu 子菜单

submenu 子菜单

submenu 子菜单

►Menu2 ►菜单2

►Menu3 ►菜单3

►Menu4 ►菜单4

As you can see the arrow with the menu1 is not down. 如您所见,菜单1的箭头没有向下。

My code till yet - 我的代码至今-

<ul>
<div class="option-heading">
  <li onclick="menu()"> //menu is the vertical menu which shows up.
      <a href="#" >
        <div class="arrow-up">&#9658;</div>
        <div style="display: none;" class="arrow-down">&#9660;</div>
      </a>
  </li>
  </div> 
</ul>    

<script>
$(document).ready(function() {
     $(".option-heading").click(function(){
     $(this).find(".arrow-up, .arrow-down").toggle();
});
});

I used .load, but it expands all the arrows at once. 我使用.load,但是它会一次扩展所有箭头。

All I want to know is the way to toggle a particular element on loading the window. 我想知道的是在加载窗口时切换特定元素的方法。 Such that other elements does not get affected by it. 这样其他元素就不会受到它的影响。 Like the present situation is with the arrows. 就像现在的情况一样,箭头是正确的。 Whenever the load event is triggered it toggles all the arrows. 每当触发加载事件时,它都会切换所有箭头。

$(this) refers to .option-heading ; $(this)指向.option-heading ; any option-heading (that's why all the arrows are toggled). 任何选项标题(这就是为什么所有箭头都被切换的原因)。 You should add a class to the active option-heading to make it unique. 您应该将一个类添加到活动的选项标题中,以使其唯一。

Something like this might do the job: 这样的事情可能会完成这项工作:

$(document).ready(function() {
   $(".option-heading").click(function(){
     $(".option-heading").removeClass("active");
     $(this).addClass("active");
     $(".option-heading.active").find(".arrow-up, .arrow-down").toggle();
   });
});

i think it's better to use one div to show a arrow, see this: 我认为最好使用一个div来显示箭头,请参见:

<div class="arrow expanded"></div>
or
<div class="arrow"></div>

in CSS style: 在CSS样式中:

div.arrow {       /* default/collapsed style */
    width: 20px;
    height: 20px;
    backgound: url('address of collapse image'); /* if use image for arrow */
    /* content: &#9658;  */ /* if use special character */
}

div.arrow.expanded { /* expanded style */
    backgound: url('address of expand image'); /* if use image for arrow */
    /* content: &#9660;  */ /* if use special character */
}

with this approach you can easily toggle arrow icon by: 通过这种方法,您可以通过以下方式轻松切换箭头图标:

$('div.arrow').toggleClass('expanded');

Or you can check expand/collapse by: 或者,您可以通过以下方式检查展开/折叠:

var isExpanded = $('div.arrow').is('.expanded');

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

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