jQuery:树菜单中的链接不';t导航,如何修复此问题?

jQuery:树菜单中的链接不';t导航,如何修复此问题?,jquery,tree,navigation,hyperlink,Jquery,Tree,Navigation,Hyperlink,我的树菜单在无序列表中。但是,其中的链接不起作用,我认为这是因为整个ul都在侦听jquery脚本。我希望链接是可导航的,有人能告诉我怎么做吗 jquery: $("#treeMenu li").toggle( function() { $(this).children('ul').slideDown() if ($(this).hasClass('contentContainer')) { $(this).removeClass('contentConta

我的树菜单在无序列表中。但是,其中的链接不起作用,我认为这是因为整个ul都在侦听jquery脚本。我希望链接是可导航的,有人能告诉我怎么做吗

jquery:

   $("#treeMenu li").toggle(

function() {
    $(this).children('ul').slideDown()
    if ($(this).hasClass('contentContainer')) {
        $(this).removeClass('contentContainer').addClass('contentViewing');
    }
}, function() {
    $(this).children('ul').slideUp()
    if ($(this).hasClass('contentViewing')) {
        $(this).removeClass('contentViewing').addClass('contentContainer');
    }
});
html:

  • folder1

来自以下文档:

由于.toggle()在内部使用单击处理程序来执行其工作,因此我们必须取消绑定单击以删除附加了.toggle()的行为,以便其他单击处理程序可以在crossfire中被捕获。该实现还对事件调用.preventDefault(),因此如果对元素调用了.toggle(),则不会跟随链接,也不会单击按钮

看起来你就是这样。您可以使用几行jQuery将单击功能添加回:

$('a').click(function() {
    location.href = $(this).attr('href');
});​
(这在小提琴上不起作用,但在你的网站上应该起作用。)

但是,我不喜欢
。toggle
会自动取消其他单击事件,这可能会让您再次感到惊讶,因此我会将您的
。toggle
替换为正确的
。单击
,然后改用:

您也可以在click处理程序中使用它来缩短代码,但我不能确定您希望如何根据您的fiddle添加或删除类


检查slideToggle和toggleClass。

确实有效。替换html文件中的js

$("#treeMenu li").click(function() {
    $(this).children('ul').slideToggle();
    if ($(this).hasClass('contentContainer')) {
        $(this).removeClass('contentContainer').addClass('contentViewing');     
    } else if ($(this).hasClass('contentViewing')) {
        $(this).removeClass('contentViewing').addClass('contentContainer');
    }
});​

JS错误,不允许您设置文本链接:

<script type="text/javascript">
    $(document).ready(function() {

    //Class 'contentContainer' refers to 'li' that has child with it.
    //By default the child ul of 'contentContainer' will be set to 'display:none'

        $("#treeMenu li").toggle(           

              function() { // START FIRST CLICK FUNCTION
                  $(this).children('ul').slideDown()
                  if ($(this).hasClass('contentContainer')) {   
                      $(this).removeClass('contentContainer').addClass('contentViewing');
                  }
              }, // END FIRST CLICK FUNCTION

              function() { // START SECOND CLICK FUNCTION
                  $(this).children('ul').slideUp()

                  if ($(this).hasClass('contentViewing')) {
                      $(this).removeClass('contentViewing').addClass('contentContainer');
                  }
                    } // END SECOND CLICK FUNCTIOn
                ); // END TOGGLE FUNCTION 

    }); // END DOCUMENT READY

</script>

$(文档).ready(函数(){
//类“contentContainer”是指具有子级的“li”。
//默认情况下,“contentContainer”的子ul将设置为“display:none”
$(“#treeemenu li”)。切换(
function(){//START FIRST CLICK函数
$(this.children('ul').slideDown()
if($(this.hasClass('contentContainer')){
$(this).removeClass('contentContainer').addClass('contentview');
}
},//结束第一次单击功能
函数(){//启动第二次单击函数
$(this.children('ul').slideUp()
if($(this).hasClass('contentview')){
$(this.removeClass('contentview').addClass('contentContainer');
}
}//结束第二次单击功能
);//结束切换函数
}); // 结束文件准备就绪

像这个站点或JQuery树菜单一样,有可下载的ZIP。

*更改标题下方页面中的JS,因为它是错误的。
这允许您设置href或link。

请在问题中输入您的代码;JSFIDLE是一个可以随时关闭的外部站点。@mblase75您完全正确,正在计划:)
$("#treeMenu li").click(function() {
    $(this).children('ul').slideToggle();
    if ($(this).hasClass('contentContainer')) {
        $(this).removeClass('contentContainer').addClass('contentViewing');     
    } else if ($(this).hasClass('contentViewing')) {
        $(this).removeClass('contentViewing').addClass('contentContainer');
    }
});​
<script type="text/javascript">
    $(document).ready(function() {

    //Class 'contentContainer' refers to 'li' that has child with it.
    //By default the child ul of 'contentContainer' will be set to 'display:none'

        $("#treeMenu li").toggle(           

              function() { // START FIRST CLICK FUNCTION
                  $(this).children('ul').slideDown()
                  if ($(this).hasClass('contentContainer')) {   
                      $(this).removeClass('contentContainer').addClass('contentViewing');
                  }
              }, // END FIRST CLICK FUNCTION

              function() { // START SECOND CLICK FUNCTION
                  $(this).children('ul').slideUp()

                  if ($(this).hasClass('contentViewing')) {
                      $(this).removeClass('contentViewing').addClass('contentContainer');
                  }
                    } // END SECOND CLICK FUNCTIOn
                ); // END TOGGLE FUNCTION 

    }); // END DOCUMENT READY

</script>