简体   繁体   English

jQuery在Wordpress上切换菜单不起作用

[英]jQuery for toggling menu on Wordpress not working

I'm working on a Wordpress site that has a sub-menu I need to open when a certain 'li' is clicked and close when it's clicked again. 我正在一个Wordpress网站上工作,该网站有一个子菜单,当我单击某个“ li”时,我需要打开该子菜单,而在再次单击它时,则需要关闭它。 I've tried several jQuery functions and nothing is working. 我已经尝试了几种jQuery函数,但没有任何效果。

I've also included the wp_enqueue_script function in the functions.php file, and I know the script referenced is working because I added a simple alert function to my created jQuery file and it works. 我还在functions.php文件中包括了wp_enqueue_script函数,并且我知道引用的脚本正在工作,因为我在创建的jQuery文件中添加了一个简单的警报函数即可。

Here is the HTML of the menu: 这是菜单的HTML:

<nav id="access" class="clearfix">
    <div class="container clearfix">
      <ul class="root">

         <li id="menu-item-19" class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-2 current_page_item menu-item-19">
         <a href="example.com/">my name</a></li>

         <li id="menu-item-17" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-17">
         <a href="example.com/?page_id=10">film</a>
             <ul class="sub-menu">
                  <li id="menu-item-43" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-43">
                  <a href="example.com/?page_id=42">calvin klein film</a></li>
                  <li id="menu-item-22" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-22">
                  <a href="example.com/?page_id=20">cam’ron</a></li>
               .
               .
               .
             </ul>

           </li>
      </ul>
    </div><!-- .container -->
</nav>

So when menu-item-17 is clicked, I need the sub-menu class to appear and then disappear on another click. 因此,当menu-item-17 ,我需要出现子菜单类,然后在再次单击时消失。

I've tried functions like this with no luck: 我试过这样的函数没有运气:

jQuery("#menu-item-17").click(function () {
$(".sub-menu").toggle("slow");
});

And in case it helps, here's what I added in the functions.php file: 如果有帮助,这是我在functions.php文件中添加的内容:

function attitude_child_scripts() {
wp_enqueue_script('toggle js', get_stylesheet_directory_uri() . '/js/toggle.js');
}
add_action('wp_enqueue_scripts', 'attitude_child_scripts');

What am I doing wrong?? 我究竟做错了什么?? Is it something Wordpress specific? 它是Wordpress特有的吗? Thanks in advance! 提前致谢!

The problem I found in your javscript is that you are trying to toggle the visibity of the submenu class but the #menu-item-17 li contains a link. 我在javscript中发现的问题是您试图切换子菜单类的可见性,但#menu-item-17 li包含一个链接。 The link will navigate to url contained within its href attribute by default. 默认情况下,该链接将导航到其href属性中包含的网址。 To prevent the link from executing its default behavior you would have to change your javascript to the code below: jsfiddle example --> http://jsfiddle.net/larryjoelane/206duwnt/ 为了防止链接执行其默认行为,您必须将javascript更改为以下代码:jsfiddle示例-> http://jsfiddle.net/larryjoelane/206duwnt/

Javascript code: JavaScript代码:

 /* 
jQuery document ready function call below is used passing $ as a parameter in order to avoid any conflicts with other Jquery Libraries loaded that might use the $ in its library
*/


jQuery(document).ready(function($){//begin document ready wrapper function to allow the use of the $

 //hide .sub-menu element on page load
 $(".sub-menu").hide();

/*
Adding e as a function parameter, e is as a variable that holds the event object. Child selector plus a added so that the preventDefault
only affects links that are a direct child of #menu-item-17.
*/
    $("#menu-item-17 > a").click(function(e){//begin click event

    /* 
    The code was added below because if you click on the #menu-item-17
    li element it contains a link which will navigate by default.
    */
    e.preventDefault();


$(".sub-menu").toggle("slow");

});//end click event

});//end document ready wrapper function

If you want to keep the link in the #menu-item-17 li element, You could place another element beside it in the li so that the user could click on the new element to toggle visibility. 如果要将链接保留在#menu-item-17 li元素中,则可以在li中的该元素旁边放置另一个元素,以便用户可以单击新元素来切换可见性。 This could be some plain text or an image. 这可能是一些纯文本或图像。 You could also move the link to another li element all together. 您也可以将链接一起移动到另一个li元素。 If you make any of these changes then you would want to remove the e.preventDefault() function call so that your link in the #menu-item-17 li element will navigate to another page again. 如果您进行了任何更改,则需要删除e.preventDefault()函数调用,以便#menu-item-17 li元素中的链接将再次导航到另一个页面。

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

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