简体   繁体   English

如何使用javascript动态更改访问链接的颜色?

[英]how to change visited link color dynamically using javascript?

I created dynamic list(list with hyperlink) using jquery.when I click that link fo rthe first time it will go to the next page. 我使用jquery创建了动态列表(带有超链接的列表)。当我第一次单击该链接时,它将转到下一页。 I used the cookie for save the index of link value while I am clicking that link.Again run that application get the saved index value from cookie in onload.Using that value change the color that particular link. 当我单击链接时,我使用cookie来保存链接值的索引。再次运行该应用程序,从onload中的cookie中获取已保存的索引值。使用该值更改特定链接的颜色。 Now I want to I will run that application again that link is displayed in red color and the other links(unvisited) are displayed in blue color. 现在,我想再次运行该应用程序,该链接以红色显示,其他链接(未访问)以蓝色显示。 How to do this? 这个怎么做?

   $(".sidemenu li ").click(function() {  
              var index = $('li').index(this); 
             // alert(index);
              checkCookie(index);
            // saveid(index);
              });

    }   

    function checkCookie(index)
    {

     var linkindexvalue=index;
     // alert(linkindexvalue);
      setCookie("indexvalue",linkindexvalue,365);


    }

    function setCookie(c_name,value,exdays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + value;
    alert(document.cookie);
    }
    $(document).ready(function(){

    var list=getCookie("indexvalue");
    if(list=='1'){
    alert(" ");

     $(".sidemenu li").css("background-color","red");

    }

  });

    function getCookie(c_name)
    {
    alert("hj");
    var value = "";
    var DocumentCookie = " " + document.cookie + ";";
    var CookieSearchStr = " " + c_name + "=";
    var CookieStartPosition = DocumentCookie.indexOf(CookieSearchStr);
    var CookieEndPosition;

    if (CookieStartPosition != -1) {
    CookieStartPosition += CookieSearchStr.length;
    CookieEndPosition = DocumentCookie.indexOf(";", CookieStartPosition);
    value = unescape(DocumentCookie.substring(CookieStartPosition, CookieEndPosition));
    }

    return value;

    }  

please guide me. 请指导我。

Thanks in advance 提前致谢

use css :visited tag. 使用css:visited标签。 Else if you want to do so by jquery without any plugin then see my implementation on jsfiddle http://jsfiddle.net/JjMAX/1/ . 否则,如果您想通过jquery来执行此操作而没有任何插件,请在jsfiddle上查看我的实现http://jsfiddle.net/JjMAX/1/

There's actually the jQuery Visited plugin that let you get the visited links in your page. 实际上有一个jQuery Visited插件 ,可让您获取页面中的已访问链接。

Once included, you can select the links and attach a class with the new colour: 包含后,您可以选择链接并为课程添加新的颜色:

$('.sidemenu li a').visited().addClass('visited');

Notice that in this case you have to add an <a> inside your <li> s, since I don't think the visited feature it's strictly related to links on a anchor, not clicks on a list item. 请注意,在这种情况下,您必须在<li>内添加<a> ,因为我认为访问的功能与锚点上的链接没有严格的联系,而不是单击列表项。

If on the contrary you need to go for the cookies options, give me some time to check your code! 相反,如果您需要使用cookie选项,请给我一些时间来检查您的代码! :) :)

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

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