【问题标题】:syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$'语法错误,意外的 '(',期望变量 (T_VARIABLE) 或 '$'
【发布时间】:2016-01-02 03:55:15
【问题描述】:

我正在尝试构建选项卡式面板,用户可以在刷新、更新等之后保留在当前选项卡上。因此,我正在尝试将此脚本作为选项卡式面板应用到我的网站,但无法弄清楚是什么原因语法错误。几天来,我一直在尝试解决有关标签的问题,但似乎可以解决。顺便说一句,我不使用 cookie 和 jquery。我不使用 cookie,因为我的朋友(我的客户)不在他们的终端上使用 cookie。其次,我不使用 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>

【问题讨论】:

  • 嗯,你在 PHP 中运行 JS,。因此,错误。这完全是两种不同的动物。
  • 想看魔术吗?将&lt;?php 替换为&lt;script&gt;,将?&gt; 替换为&lt;/script&gt; ;-)
  • 这是 all jQuery。 对拉尔夫? @Fred-ii-
  • Si signore Sam @JayBlanchard 嘿……你在这干什么?啊,我敢打赌是一部深夜电影。关于 Wile E. 获得 Road Runner 的那个?
  • 10-4 拉尔夫! @Fred-ii-

标签: javascript php


【解决方案1】:

您在 PHP 中使用 Javascript(准确地说是 jQuery)语法。因此,错误。

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

  • 这完全是两种不同的动物。

&lt;?php 替换为&lt;script&gt;,将?&gt; 替换为&lt;/script&gt;,您的代码将正常运行。

【讨论】:

  • 哇,你永远是天才。 .
  • @devpro 嘿,谢谢。 干杯祝你新年快乐!
  • @devpro 那应该是另一部很棒的电影。我一定会留意的 ;-) 一定要爱上那个人。
  • @devpro 就在上面。总是很高兴知道我能够从另一个人身上擦掉其中的一些,并保持 Stack 应有的质量网站。一切顺利。
  • tq @Fred-ii-。其实我有点郁闷,因为我突然被ban了(不知道为什么)..btw..tq这么多...
猜你喜欢
  • 1970-01-01
  • 2018-12-16
  • 2018-07-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-11
  • 2015-11-19
  • 2021-11-03
  • 2015-03-09
相关资源
最近更新 更多