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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大