doulangxun7769 2015-12-05 12:15
浏览 106
已采纳

满足某些php条件时动态激活菜单

I want to trigger a tab to be active when some php conditions are met.

This is the bootstrap code I am using, it is a nav-pills and Toggable / Dynamic.

https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>

<ul class="nav nav-pills">
  <li class="active"><a data-toggle="pill" href="#home">Home</a></li>
  <li><a data-toggle="pill" href="#menu1">Menu 1</a></li>
  <li><a data-toggle="pill" href="#menu2">Menu 2</a></li>
</ul>

<div class="tab-content">
  <div id="home" class="tab-pane fade in active">
    <h3>HOME</h3>
    <p>Some content.</p>
  </div>
  <div id="menu1" class="tab-pane fade">
    <h3>Menu 1</h3>
    <p>Some content in menu 1.</p>
  </div>
  <div id="menu2" class="tab-pane fade">
    <h3>Menu 2</h3>
    <p>Some content in menu 2.</p>
  </div>
</div>

By default Home tab is active. Before the page is loaded, I want to check if a global variable i.e $_SESSION['variable1'] is set. If it is not set, then I should set menu2 to be active.

At the top of the code, I tried doing this and it didn't work

if (!isset($_SESSION['variable1'])){

echo " <script type=\"text/javascript\"> $('.nav-pills
a[href=\"#menu2\"]').tab('show') </script>";

}

But if I inspect element, and paste the JavaScript code above in the console, It work! I can't seem to be able to run it in my code though. Home tab stay active.

</div>
  • 写回答

3条回答 默认 最新

  • douke8473 2015-12-05 12:40
    关注

    You are executing the script before the HTML is loaded.

    I've tried it, and it works if you put the PHP code at the end of the <body>.

            ...
            </div>
        </div>
        <?php
        if (!isset($_SESSION['variable1'])){
    
            echo " <script type=\"text/javascript\"> $('.nav-pills
            a[href=\"#menu2\"]').tab('show') </script>";
    
        }
        ?>
    </body>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算