简体   繁体   English

切换多个jQuery图标

[英]Toggling multiple jQuery icons

I'm having a few problems with my icon toggling in jQuery. 我在jQuery中切换图标时遇到一些问题。

The html for each toggled section is: 每个切换部分的html为:

<h2 class="collapse"><span class="ui-icon ui-icon-triangle-1-e"></span>Section 1.1</h2>
<p>Hidden text.</p>

And the javaScript: 和javaScript:

jQuery(document).ready(function () {
    $(".collapse").click(function () {
        $(this).next().toggle();
        $("span").toggleClass("ui-icon-triangle-1-s");
    }).next().hide();
});

JSfiddle also here . JSfiddle也在这里

When I click any of the collapsible sections, all of the icons change at once, though not all of them expand/collapse. 当我单击任何可折叠部分时,所有图标都立即更改,尽管并非全部都展开/折叠。

I also can't get my icon to be on the same line as the text, though that's a smaller problem. 尽管这是一个较小的问题,但我也无法使我的图标与文本位于同一行。

You are targeting all span elements.you need to only target the child span element of clicked h2 element: 您要定位所有span元素。您只需定位被单击h2元素的子span元素:

jQuery(document).ready(function () {
$(".collapse").click(function () {
    $(this).next().toggle();
    $(this).find('span').toggleClass("ui-icon-triangle-1-s");
}).next().hide();
});

and for aligning the icon set style float to left : 并且为了使图标集样式left float

 .ui-icon-triangle-1-e{float:left;}

Working Demo 工作演示

There you go : http://jsfiddle.net/gy653bpL/15/ . 你去了: http : //jsfiddle.net/gy653bpL/15/

add some CSS : 添加一些CSS:

.collapse span {display:inline-block;}

toogle only the active span : 仅激活活动范围:

$(this).find("span").toggleClass("ui-icon-triangle-1-s");

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

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