语法错误,意外'(',期待变量(T_VARIABLE)或'$'

时间:2016-01-02 03:55:15

标签: javascript php

我正在尝试构建选项卡式面板,用户可以在刷新,更新等后保留在当前选项卡中。因此,我正在尝试将此脚本应用于我的Web作为选项卡式面板但无法找出原因语法错误。我一直试图解决问题几天关于选项卡,但似乎可以解决它。顺便说一下,我不使用cookies和jquery。我不使用cookies,因为我的朋友(我的客户)不在他们的终端上使用cookies。第二,我不使用jquery(即使它更好)我只喜欢php。请帮忙谢谢。

<?php 
 $('ul.tabs').each(function(){
    // For each set of tabs, we want to keep track of
    // which tab is active and its associated content
    var $active, $content, $links = $(this).find('a');
    // If the location.hash matches one of the links, use that as the active tab.
    // If no match is found, use the first link as the initial active tab.
    $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
    $active.addClass('active');
    $content = $($active[0].hash);
    // Hide the remaining content
    $links.not($active).each(function () {
      $(this.hash).hide();
    });
    // Bind the click event handler
    $(this).on('click', 'a', function(e){
      // Make the old tab inactive.
      $active.removeClass('active');
      $content.hide();
      // Update the variables with the new link and content
      $active = $(this);
      $content = $(this.hash);
      // Make the tab active.
      $active.addClass('active');
      $content.show();
      // Prevent the anchor's default click action
      e.preventDefault();
    });
  }); ?>
<html>
<head>
</head>
<body>
<ul class='tabs'>
    <li><a href='#tab1'>Tab 1</a></li>
    <li><a href='#tab2'>Tab 2</a></li>
    <li><a href='#tab3'>Tab 3</a></li>
  </ul>
  <div id='tab1'>
    <p>Hi, this is the first tab.</p>
  </div>
  <div id='tab2'>
    <p>This is the 2nd tab.</p>
  </div>
  <div id='tab3'>
    <p>And this is the 3rd tab.</p>
  </div>
  </body>
  </html>

1 个答案:

答案 0 :(得分:2)

你在PHP中使用Javascript(jQuery准确)语法。因此,错误。

  

语法错误,意外'(',期待变量(T_VARIABLE)或'$'

  • 这些是两种不同的动物。

<?php替换为<script>,将?>替换为</script>,您的代码将有效。