简体   繁体   English

使用jquery为wordpress创建手风琴,需要帮助!

[英]Creating an accordion for wordpress with jquery, need help!

I'm creating an accordion for wordpress and I can't seem to figure what I am doing wrong. 我正在为Wordpress创建一个手风琴,但似乎无法弄清自己在做什么错。

Ultimately I am trying to have the child elements hidden, and when the parent has been activated the child will appear. 最终,我试图隐藏子元素,当父元素被激活时,子元素将出现。

JS: JS:

<script>
    $(document).ready(function() { //hide child page
        $('#content ul.menu ul li').hide();
        //when navigate to a child page show all pages
        $('#content ul.menu li.current-menu-item').parent("ul").show();
        //show page list when toggle
        $('#content ul.menu ul li.active ul').show();
        //show cerrent page's child page
        $('#content ul.menu ul li.current-menu-item ul').show();

        $('#content .menu ul li').click(function() {
            $(this).addClass("active");
            $(this).children('ul').slideToggle("slow");
        });
    });
</script>

The site: www.svadsi.info 该网站: www.svadsi.info

Thanks in advance. 提前致谢。

Wordpress doesn't always play nice with jquery. Wordpress并不总是与jquery配合使用。

First you want to wrap your code in 首先,您要包装代码

$(function() {

instead of 代替

$(document).ready(function() {

Also, I would recommend using 另外,我建议使用

.addClass('hidden');

and

removeClass('hidden');

instead of .hide(); 而不是.hide(); and .show(); 和.show(); when dealing with the same problem. 当处理相同的问题时。

Please feel free to follow up with comments and I'll help you troubleshoot. 请随时跟进评论,我们将帮助您解决问题。

Can you explain you problem more please. 您能再解释一下您的问题吗?

You should caught the click event on li cause it bubble 您应该抓住li上的click事件,导致它冒泡

    $('#content .menu ul li').click(function(e) {
        $(e).stopPropagation();
        $(this).addClass("active");
        $(this).children('ul').slideToggle("slow");
    });

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

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