简体   繁体   English

菜单onclick功能未显示

[英]menu onclick function not being seen

I'm trying to change the menu button colour based on which page of the site the person clicks on. 我正在尝试根据此人单击的站点页面来更改菜单按钮的颜色。 The function onclick isn't being called at all when I click on the menu links (I've tried a console.log()). 当我单击菜单链接时,根本没有调用onclick函数(我尝试过console.log())。 I'm using a header.php file to load the header into each page so I can't change the class based on which page is loaded through just html. 我正在使用header.php文件将标头加载到每个页面中,因此无法基于仅通过html加载哪个页面来更改类。 I'm using jquery: 我正在使用jquery:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

In the header.php file: 在header.php文件中:

       <nav id = "menu">
            <ul class="nav">
                <li><a class="toggle-colour" href = "index.php">Home</a></li>
                <li><a class="toggle-colour" href = "work.php">Work</a></li>
                <li><a class="toggle-colour" href = "clients.php">Clients</a></li>
                <li><a class="toggle-colour" href = "contact.php">Contact</a></li>

            </ul>
        </nav>

JS: JS:

 $(document).ready(function() {

  $('.nav li a').on('click', function() {
      $(this).toggleClass("selected");  
    });
});

css 的CSS

 a { color: #fff; }
a.selected {
        color: #444;
    }

In the header php file, why don't you do check the page and then apply the class? 在标头php文件中,为什么不检查页面然后应用该类?

<nav id = "menu">
            <ul class="nav">
                <li><a class="toggle-colour <? if($_SERVER['PHP_SELF'] == "/index.php"){ echo "selected"; } ?>" href = "index.php">Home</a></li>
                <li><a class="toggle-colour <? if($_SERVER['PHP_SELF'] == "/work.php"){ echo "selected"; } ?>" href = "work.php">Work</a></li>
                <li><a class="toggle-colour <? if($_SERVER['PHP_SELF'] == "/clients.php"){ echo "selected"; } ?>" href = "clients.php">Clients</a></li>
                <li><a class="toggle-colour <? if($_SERVER['PHP_SELF'] == "/contact.php"){ echo "selected"; } ?>" href = "contact.php">Contact</a></li>

            </ul>
    </nav>

... so as the others point out, when you change pages the javascript code does not follow. ...正如其他人指出的那样,当您更改页面时,不会遵循javascript代码。 If you want to highlight the page they are on when they change you can do something like the above. 如果要在更改页面时突出显示它们所在的页面,可以执行上述操作。 The above checks the page and applies the class if they are on that page. 上面的代码检查页面并应用该类(如果它们在该页面上)。

Try the below code, 试试下面的代码,

Your code won't work since it's redirecting to another page 您的代码无法重定向 ,因为它已重定向到另一个页面

 $(document).ready(function() { $('.nav li a').on('click', function() { $(this).toggleClass("selected"); return false; }); }); 
 a { color: #ddd; } a.selected { color: red; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <nav id="menu"> <ul class="nav"> <li><a class="toggle-colour" href="">Home</a> </li> <li><a class="toggle-colour" href="">Work</a> </li> <li><a class="toggle-colour" href="">Clients</a> </li> <li><a class="toggle-colour" href="c">Contact</a> </li> </ul> </nav> 

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

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