doupeng6890
2016-12-19 06:08
浏览 42
已采纳

使用jquery和PHP添加类时出现问题

To start off, if this has already been answered please point me to the right area as I have not yet been able to find it.

I have built a web sit which contains 4+ pages and I am using the php code include(filename.php); where filename.php is the name of the php file containing my header navigation. I also have a class named "current" which, before adding the include() statement was manually placed on each link within the navigation bar. I am trying to do this using jQuery as I have had to remove this class within the linked php file. I have tried writing the code as such:

<body>
<?php
    $file_included = true;
    // common code used in every page
    if ($file_included == true) {
        include("header.php");
    } else {
        header("Location:remedies.php");
    }
?>
<script>
    $(document).ready(function() {
        $("#about").addClass("current");
    });
</script>

however when I test this site on my server, the "current" class does not move to the currently selected link. The code contained within the linked .php file is as follows:

<div id="title">
<header>
    <img src="images/Dragon-Catcher-Web-Logo.jpg" alt="Dragon Catcher Web Design Logo" id="design" style="padding-right:  15%; padding-left: 1%; padding-top: 1%;"/>
    <span style="text-align: center;">Dragon Catcher Herbs</span>
</header>
<nav>
    <ul id="navlist">
        <li><a href="index.php" id="home">Home</a></li>
        <li><a href="about.php" id="about">About Us</a></li>
        <li><a href="contact.php" id="contact">Contact Us</a></li>
        <li><a href="beginner.php" id="beginner">Beginner Herbalists</a></li>
        <li><a href="herbs.php" id="herb">Herb List</a></li>
        <li><a href="remedies.php" id="remedy">Remedies</a></li>
        <li><a href="recommend.php" id="user">User Recommended</a></li>
    </ul>
</nav>

not sure where I went wrong with my code but any and all help would be great.

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqwh1219 2016-12-19 06:28
    已采纳

    I used your code and make two separate files as below:

    question.php

    <html>
    <head>
    <script
      src="https://code.jquery.com/jquery-2.2.4.min.js"
      integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
      crossorigin="anonymous"></script>
    </head>
    <body>
    <?php
        include("header.php");
    ?>
    <script>
        $(document).ready(function() {
            $("#about").addClass("current");
        });
    </script>
    </body>
    </html>
    

    header.php

    <nav>
        <ul id="navlist">
            <li><a href="index.php" id="home">Home</a></li>
            <li><a href="about.php" id="about">About Us</a></li>
            <li><a href="contact.php" id="contact">Contact Us</a></li>
            <li><a href="beginner.php" id="beginner">Beginner Herbalists</a></li>
            <li><a href="herbs.php" id="herb">Herb List</a></li>
            <li><a href="remedies.php" id="remedy">Remedies</a></li>
            <li><a href="recommend.php" id="user">User Recommended</a></li>
        </ul>
    </nav>
    

    And css class current applies on #about. I think you didn't included jquery.js in your code. Just try this code.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题