简体   繁体   English

与jQuery简单的手风琴?

[英]simple accordion with jquery?

Here is my jquery 这是我的jQuery

$(document).on('click', '.lists', function() {
    $(this).find('hide-me').attr('hide', 'show');
    console.log('test');
});

How can I change the class hide-me to show-me 如何将我的班级“隐藏我”更改为“显示我”

<div class="lists">
    <h5 class="head">header</h5>
    <ul class="list hide">
    <li>test</li>
    <li>test</li>
    <li>test</li>
    </ul>
</div>

I tried the above jquery but i does nothing. 我尝试了上面的jQuery,但我什么也没做。 I need to rename the class . 我需要rename the class Where is the mistake? 错误在哪里?

Try this: 尝试这个:

$(document).on('click', '.lists', function() {
    $(this).find('.hide').addClass('show').removeClass('hide')
});

If I understand your intents correctly you probably want to toggle the hide and show, then you should be able to do it like this: 如果我正确理解了您的意图,则可能要切换隐藏和显示,那么您应该能够做到这一点:

$(document).on('click', '.lists', function() {
    $(this).parent().find(".lists ul").removeClass("show").addClass("hide");
    $(this).find('.hide, .show').toggleClass('show hide');
});

EDIT: I updated some classes to be the same as in your html. 编辑:我更新了一些类,使其与您的html中的相同。 See working fiddle here: http://jsfiddle.net/rrAhX/6/ 参见此处的工作提琴: http : //jsfiddle.net/rrAhX/6/

If there is a reason that you can't use isherwood 's .slideToggle() suggestion (like other things are tied to the classes, or something), then you would use the .toggleClass() : http://api.jquery.com/toggleClass/ 如果由于某种原因不能使用isherwood.slideToggle()建议(就像其他东西绑定到了类之类的东西一样),则可以使用.toggleClass()http://api.jquery .com / toggleClass /

For your code specifically, change this: 专门针对您的代码,更改以下内容:

$(this).find('hide-me').attr('hide', 'show');

. . . to this: 对此:

$(this).find('hide-me').toggleClass('hide-me show-me');

This will toggle both classes, independently of each other, so as long as you start out with only one of the two classes on your element, it will remove that one, and add the other. 这将相互独立地切换两个类,因此,只要您仅从元素上两个类之一开始,它将删除该一个,然后添加另一个。 Click again and the first one will be re-added and the second one removed. 再次单击,第一个将被重新添加,第二个将被删除。

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

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