简体   繁体   English

记住最近的点击

[英]remember most recent click

So I want to be able to have a different styling for a link after you go to the page it's clicked on. 因此,我希望在您转到单击页面后能够为链接设置不同的样式。 I have been using the following code: 我一直在使用以下代码:

$(document).ready(function(){
var url = document.URL;
function contains(search, find) {
    return search.indexOf(find) !== -1;
};
$('#topbar a').each(function(){
    var link = $(this).attr('href');
    var answer = contains(link,url);
    if(answer === true){
        $(this).addClass('check');
    }
    else{
        $(this).addClass('nocheck');
    };
});
});

This goes through the links in my navigation bar and checks if it's on the same page as the link, and it works, but I can't use it in one specific case: Random. 这通过导航栏中的链接检查它是否与链接在同一页面上,并且它有效,但我无法在一个特定情况下使用它:随机。

I have a link that generates a random page from the pages I have, so it does not have a specified link as it links to a function to randomly generate the page (note: I cannot change the function or access information from it). 我有一个链接,从我的页面生成一个随机页面,因此它没有指定的链接,因为它链接到一个函数来随机生成页面(注意:我无法更改功能或访问它的信息)。

So how can I detect that the random link was clicked previously so i can give it the .check class 那么我怎么能检测到之前点击了随机链接所以我可以给它.check

If i understand your question correctly, your function does not work for the random link because this has a href like http://mysite.com/random , but the server will actualy redirect you to a different page, like http://mysite.com/about-me , and therefore the url of the active page does not match the href of the random button, and it will not get the active state. 如果我正确理解你的问题,你的函数不适用于random链接,因为它有像http://mysite.com/random这样的href ,但是服务器会实际上将你重定向到另一个页面,比如http://mysite.com/about-me ,因此活动页面的url与随机按钮的href不匹配,并且它不会获得活动状态。

One could argue if you would want it to get the active state, cause clicking it again would not (likely) bring you to the same page, but that is besides the question. 有人可能会争辩说你是否希望它获得活跃状态,因为再次点击它不会(可能)带你到同一页面,但这就是问题。

I can see to ways to solve this. 我可以看到解决这个问题的方法。

server side: 服务器端:
In stead of redirecting to ie. 而不是重定向到ie。 http://mysite.com/about-me in the random function, you could also redirect to http://mysite.com/about-me?random . http://mysite.com/about-me在随机函数中,你也可以重定向到http://mysite.com/about-me?random By adding this get variable, you should not change the behaviour of the link (unless you have some very strict controller, or that variable is actually used, but that is unlikely). 通过添加此get变量,您不应该更改链接的行为(除非您有一些非常严格的控制器,或者实际使用该变量,但这不太可能)。 You could then detect with javascript if that variable is present in the url, and then activate the random button. 然后,您可以使用javascript检测该变量是否存在于URL中,然后激活random按钮。

Something like this: 像这样的东西:

$(document).ready(function(){
  var url = document.URL;
  // check for random
  if (url.indexOf('?random') >= 0) {
     $('#topbar a.random').addClass('check');
  }
  // check all other
  $('#topbar a:not(.random)').each(function(){
      if($(this).attr('href').indexOf(url) >= 0){
         $(this).addClass('check');
      }
      else{
         $(this).addClass('nocheck');
      };
  });
});

cookie: 曲奇饼:
If you do not have acces to the server side random controller, you could do it entirely with javascript, by the use of a cookie (the only way I know to make a variable persist trough page requests). 如果你没有访问服务器端随机控制器,你可以通过使用cookie完全使用javascript(我知道通过页面请求保持变量的唯一方法)。

On click of the random button, you would first set a random cookie to true with javascript, before letting the actual link do it's thing. 单击random按钮,您将首先使用javascript将random cookie设置为true,然后让实际链接执行此操作。 On entering the page, you could then do a similar check as in my previous option, but in stead of the url you check if the cookie is tre. 在进入页面时,您可以执行与我之前的选项类似的检查,但是您可以检查cookie是否为tre,而不是网址。 If so, you change it to false (so on the next page request the random button will not be active again) and set the random button to active. 如果是这样,您将其更改为false(因此在下一页请求时,随机按钮将不再处于活动状态)并将random按钮设置为活动状态。

As I believe the first solution is to be preferred (cookies should only be used as a last resort, they are sent on every page request, which means extra data, and your user might have cookies disabled, or there might be laws against using cookies, so the function could not always work), I will not write the javascript yet. 我相信第一个解决方案是首选(Cookie应该只作为最后的手段使用,它们会在每个页面请求时发送,这意味着额外的数据,并且您的用户可能禁用了Cookie,或者可能存在禁止使用Cookie的法律,所以函数不能总是工作),我不会写javascript了。 Feel free to ask if you prefer this solution and need further help however. 请随意询问您是否更喜欢此解决方案,但需要进一步的帮助。

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

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