JQuery - 如何使下一个父链接扩展自己的子链接而不是让它转到下一页?

时间:2012-03-16 09:22:25

标签: jquery wordpress

我在wordpress上工作,我通过谷歌找到它。那么让我困惑的唯一事情是如何让父链接被允许扩展子菜单而不是让父链接可点击..

jQuery(document).ready(function() {

//Hide the submenus
jQuery('.leftNav_Level2 ul li ul').hide();

//If its a page parent (based off wordpress), add the class "displayMe"
//This way the accordion will be opened up on the page you are on.
if (jQuery('.leftNav_Level2 ul li').hasClass("current_page_parent")) {
jQuery('.leftNav_Level2 .current_page_parent ul').addClass("displayMe");
jQuery('.leftNav_Level2 .current_page_ancestor > ul').attr('style','display:block');
jQuery('.leftNav_Level2 .current_page_parent > ul').attr('style','display:block');
}
jQuery('.leftNav_Level2 .current_page_item > ul').attr('style','display:block');


//When you click it, toggle
jQuery('.leftNav_Level2 ul li a').click(
function() {

//Onclick Remove the class dipslay me which is only display:block;
//This way they can close it if they click it or it will glitch
jQuery(this).next().next().slideToggle('slow').removeClass("displayMe");

//return false so the # doenst move view to the top of the page
if (jQuery(this).attr('href') == '#') { return false; }

//Close it all out
});
}); 

wordpress模板有:

  <div class="leftNav_Level2">
<ul>
<?php wp_list_pages('title_li='); ?>
</ul>
</div><!--/leftNav_Level1-->

非常感谢帮助,谢谢你

1 个答案:

答案 0 :(得分:0)

您使用了太多的JavaScript来解决问题。我会说,使用CSS作为样式(打开/关闭子菜单等),这方面的教程在这里:http://wpguru.co.za/navigation/make-your-own-wordpress-drop-down-menu/

如果您希望父节点停止导航到链接页面,您可以使用:

jQuery('.leftNav_Level2 ul>li.current_page_ancestor').click(function(e) {
    e.preventDefault();
});

您还可以查看自WP 3.0以来的新菜单,更多信息请点击此处:http://bavotasan.com/2010/wordpress-3-checking-out-the-new-menu-system/