简体   繁体   English

CSS菜单项选择问题jQuery / cakephp

[英]Css menu item selection issue jQuery/cakephp

Hi all I am developing a application in cakephp and have defined a element menu_top.ctp as below: 大家好,我正在cakephp中开发应用程序,并定义了一个元素menu_top.ctp,如下所示:

<script type="text/javascript">var path =location.pathname;

` `

    $(document).ready(function () {
        alert(path);
        /* select the menu nav tab based on the url */
        $("#menu li a[href='" + [ path ] + "']").parents("li").each(function() { 
            $(this).addClass("current-menu-item");
        });

    });
</script>
<div id="welcomeMsg">
<?php 

    if($session->check('Auth.User')):
     echo "<span style='float:right;color:#ffffff;'> Welcome ".$session->read('Auth.User.username')."</span>";
    endif       
?>
</div>
<div style="clear:both;"></div>
<div id="topNav"> 
    <?php 
        if($session->check('Auth.User')): ?>
            <ul id="menu" style="margin-top:10px;">
                <li>
                  <?php echo $this->Html->link('Home',array('controller' => 'pages', 'action' => 'display', 'home')); ?>
                </li>
                    <?php if($session->read('Auth.User.admin') == 1): ?>
                 <li class="">
                    <?php echo $this->Html->link('Admin',array('controller' => 'admin','action'=>'index')); ?>
                </li>
                <?php endif ?>
                <li class="">
                  <?php echo $this->Html->link('Terms & Conditions/Privacy Policy',array('controller' => 'toc','action'=>'index')); ?>
                 </li>

                <li class="">
                    <?php echo $this->Html->link('Contact Us',array('controller' => 'contact','action'=>'index')); ?>
                </li>
                <li class="">
                     <?php echo $this->Html->link('About Us',array('controller' => 'about','action'=>'index')); ?>
                </li>
                  <li class="">
                     <?php echo $this->Html->link('Log Out',array('controller' => 'users','action'=>'logout')); ?>
                </li>
            </ul>

The jquery code defined above selects menu item when top level url are opened in browser for eg. 例如,当在浏览器中打开顶级URL时,上面定义的jquery代码选择菜单项。 http://localhost/mysite/admin will select admin tab but when I open http://localhost/mysite/admin/users the selection is lost. http://localhost/mysite/admin将选择“管理”选项卡,但是当我打开http://localhost/mysite/admin/users ,选择将丢失。 Please help me to solve this issue. 请帮我解决这个问题。 Thank you. 谢谢。

In document.ready function, change the selector from document.ready函数中,将选择器从

"#menu li a[href='" + [ path ] + "']" 

to

"#menu li a[href^='" + [ path ] + "']"

(notice the ^ sign before =) (注意=之前的^符号)

Source: http://www.w3.org/TR/css3-selectors/#attribute-substrings 来源: http//www.w3.org/TR/css3-selectors/#attribute-substrings

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

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