简体   繁体   English

PHP页面名称变量+ jQuery

[英]PHP Page name variable + jQuery

I'm having a problem, after implementing a jQuery in-page tab navigation system, with how to redefine my page names, so that they can be styled as "active" in my main navigation. 在实现jQuery页内标签导航系统后,我遇到了一个问题,该问题在于如何重新定义我的页面名称,以便可以在主导航中将其设置为“活动”样式。

I use: 我用:

<?php $var_page_name = 'example.php'; ?> 

to define each page name on my web site, with example replaced with the correct page name. 定义我网站上的每个页面名称,并用正确的页面名称替换示例。

I then use the following to style the "active" page in the navigation, with the active class defined using CSS: 然后,我使用以下代码在导航中设置“活动”页面的样式,并使用CSS定义活动类:

<?php global $var_page_name; ?>
<ul>
<li><a<? if($var_page_name == 'index.php') print ' class="active"'; ?> href="index.php">web design</a></li>
</ul>

Works great. 效果很好。 I've added an in-page jQuery tabbing solution for navigation. 我为导航添加了页内jQuery选项卡解决方案。 Which also works. 这也有效。

However, after clicking on one of the tabs and going to the new content div, its seems that the page name changes, so the correct main navigation item is no longer considered active. 但是,在单击选项卡之一并转到新的内容div之后,页面名称似乎已更改,因此正确的主导航项不再被认为是活动的。 The url reads as http://www.example.com/index.php# , but if I modify my active page navigation variable to read: 网址为http://www.example.com/index.php# ,但是如果我将活动页面导航变量修改为:

<a<? if($var_page_name == 'index.php' || $var_page_name == 'index.php#' ) print ' class="active"'; ?>

or try the div id that is being used by the tabs: 或尝试使用标签所使用的div ID:

<a<? if($var_page_name == 'index.php' || $var_page_name == 'index.php#content_2' ) print ' class="active"'; ?>

it doesn't fix the problem. 它不能解决问题。 Any suggestions? 有什么建议么?

It's a bug in your tabs script. 这是您的tab脚本中的错误。

When you click a tab it removes the "active" class from your <a> in your top nav even though that <a> is not a tab. 当您单击选项卡时,即使<a>不是选项卡,它也会从顶部导航栏中的<a>中删除“活动”类。

Change the line: 更改行:

// switch all tabs off
$(".active").removeClass("active");

to: 至:

$(".tabs .active").removeClass("active");

or something similar to that which only removes the class 'active' from your tabs instead of all elements in your page. 或类似于仅从选项卡中删除“活动”类而不是页面中所有元素的类。

<?php $var_page_name = 'example.php'; ?>

that's wrong. 错了 Use something like 使用类似

<?php
$exploded = explode($_SERVER['REQUEST_URI']);
<a href="/page/" class="<?php echo ($exploded[1] == "page") ? "active" : ""; ?>">Page</a>

Keep in mind that hash isn't being sent to the server by default 请记住,默认情况下不会将哈希发送到服务器

Well, I think the problem is more related with the jquery code in the page, than the PHP code. 好吧,我认为问题与页面中的jquery代码有关,而不是与PHP代码有关。 Checking the js header code, I found this: 检查js标头代码,我发现了这一点:

// When a link is clicked       
$("a.tab").click(function () {                              
  // switch all tabs off            
  $(".active").removeClass("active");
  ...
});

The click binding on the selector "a.tab" is clearing all ".active" classes in the whole page, including the ones in the "nav" section (wich is above the tab). 选择器“ a.tab”上的单击绑定将清除整个页面中的所有“ .active”类,包括“ nav”部分中的类(位于选项卡上方)。
Try changin this line: 尝试更改此行:

$(".active").removeClass("active");

To this: 对此:

$("#tabbed_box_1").find(".active").removeClass("active");

That should do it. 那应该做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM