代码之家  ›  专栏  ›  技术社区  ›  user3052443

如何在jQuery中确定活动选项卡

  •  0
  • user3052443  · 技术社区  · 6 年前

    我试图用一个php值来控制标签页上显示的内容。我的基本代码如下所示。就切换而言,选项卡工作正常。但是tab代码并不控制php代码,因此它们都会显示,而不是一个tab显示的内容。有没有办法用这样的php来控制所显示的内容?

        <div id="handle-tabs" style="overflow: auto;">
          <ul>
            <li><?php echo '<a href="example.com?section-one">Section One</a>'; ?></li>
            <li><?php echo '<a href="example.com?section-two">Section Two</a>'; ?></li>
            <li><?php echo '<a href="example.com?section-three">Section Three</a>'; ?></li>
          </ul>
    
          <div id="section-one"> 
              <?php 
              $this_group = 'one';
              DoSomething(1);
              ?> 
          </div>
    
          <div id="section-two"> 
              <?php 
              $this_group = 'two';
              DoSomething(2);
              ?> 
          </div>
    
          <div id="section-three">   
              <?php 
              $this_group = 'three';
              DoSomething(3);
              ?>  
          </div>    
        </div>
    
        function DoSomething($id) {
          switch ($id) {
           case 1: echo 'one'; break; 
           case 2: echo 'two'; break;
           case 3: echo 'three'; break;
          } 
        }        
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   random_user_name Falguni Panchal    6 年前

    你必须通过查看 $_GET 正在传递的变量。

    然后,在 DoSomething

    <div id="handle-tabs" style="overflow: auto;">
      <ul>
        <li><?php echo '<a href="example.com?section=1">Section One</a>'; ?></li>
        <li><?php echo '<a href="example.com?section=2">Section Two</a>'; ?></li>
        <li><?php echo '<a href="example.com?section=3">Section Three</a>'; ?></li>
      </ul>
    
      <div id="section-one"> 
          <?php 
          $this_group = 'one';
          DoSomething(1);
          ?> 
      </div>
    
      <div id="section-two"> 
          <?php 
          $this_group = 'two';
          DoSomething(2);
          ?> 
      </div>
    
      <div id="section-three">   
          <?php 
          $this_group = 'three';
          DoSomething(3);
          ?>  
      </div>    
    </div>
    
    function DoSomething( $id ) {
      // load the current tab ID into the $current variable
      $current = (int)( isset( $_GET['section'] ) ) ? $_GET['section'] : '';
    
      // if the tab to be displayed is not the "current" one, then do NOT render anything
      if ( $id != $current ) {
          return;
      }
    
      switch ( $id ) {
       case 1: 
           echo 'one'; 
           break; 
       case 2: 
           echo 'two'; 
           break;
       case 3: 
           echo 'three'; 
           break;
      } 
    }