简体   繁体   English

JavaScript特定属性更改

[英]Javascript Specific Attribute Altering

I have a sitemenu of which has links(ofcourse it does, its a blinking sitemenu), they only change colour when hovered over, these links are controlled by my sitemenu.php which is included in each page for easy alteration to the sitemenu.. My perdicament is that now I'd like it so that when you're on a page, the link which took you there is coloured until you leave that page. 我有一个站点菜单,该站点菜单具有链接(当然,它确实是一个闪烁的站点菜单),它们只有在悬停时才会更改颜色,这些链接由我的sitemenu.php控制,该文件包含在每个页面中,以便于更改站点菜单。我的困惑是,现在我想要它,这样当您在页面上时,将带您到那里的链接会变成彩色,直到您离开该页面为止。 Which I can't do manually because all of the links are on one primary php script which if altered alters the rest in the same fashion. 我无法手动执行此操作,因为所有链接都位于一个主要的php脚本中,如果更改,则会以相同的方式更改其余的内容。

I know how to add a class which will permanately colour a link but have no clue how to do the javascript though I think it would just use the current page url, find that in the sitemenu and add a class attribute. 我知道如何添加一个将永久为链接着色的类,但是不知道如何使用JavaScript,尽管我认为它将仅使用当前页面的url,在sitemenu中找到它并添加class属性。

You can do this with your PHP script or with your Javascript function. 您可以使用PHP脚本或Javascript函数来执行此操作。

PHP: PHP:

In your sitemenu.php, do something like this where your menu is - 在您的sitemenu.php中,执行以下操作-

<li><a href="index.php"<?php if($page == 'index') echo ' class="active"'; ?>>Home</a></li>
<li><a href="b.php"<?php if($page == 'b') echo ' class="active"'; ?>>B</a></li>
<li><a href="c.php"<?php if($page == 'c') echo ' class="active"'; ?>>C</a></li>
<li><a href="d.php"<?php if($page == 'd') echo ' class="active"'; ?>>D</a></li>

Now, in each of your pages, you will just have to set $page before including your sitemenu.php file. 现在,在每个页面中,只需要设置$ page,然后再包含sitemenu.php文件即可。

For example in your index page do something like 例如,在您的索引页面中执行以下操作

$page = 'index';
include('sitemenu.php');

Unless you are doing AJAX with your Javascript, you will have to parse out what page you are on, and then change the style of the link. 除非您使用Javascript做AJAX,否则您将不得不解析出您所在的页面,然后更改链接的样式。 If you are using AJAX for each link click, all you would have to do is inside of your onclick function use 如果您对每次链接单击都使用AJAX,则只需在onclick函数中使用

this.style.color='somecolor'

Hope this answered your question. 希望这回答了您的问题。 Just personal preference here, but I like going with the PHP route. 这里只是个人喜好,但我喜欢使用PHP路由。 Makes it really easy to edit a nav anytime adding/removing items. 使添加或删除项目的导航变得非常容易。

How about adding some css rule like a:visited{color:#eeeeee;} or whatever? 如何添加一些css规则,例如a:visited{color:#eeeeee;}或其他? If you came from a certain page and there is a link in the menu who points to this page, this link should have the pseudo selector visited. 如果您来自某个页面,并且菜单中有一个指向该页面的链接,则此链接应访问伪选择器。

This should give you the basic idea (using jQuery/JavaScript). 这应该给您基本的想法(使用jQuery / JavaScript)。 It adds the active class to the current page. 它将active类添加到当前页面。 You may have to adjust a bit depending on your site's linking conventions: 您可能需要根据网站的链接约定进行一些调整:

var url = document.location.pathname.toString();

$('a').each( function() {
    // if it matches
    if ($(this).attr('href').indexOf(url) != -1) {
       $(this).addClass('active');
    }
});

Here's a working demo . 这是一个工作示例

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

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