簡體   English   中英

如何使用javascript動態更改訪問鏈接的顏色?

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

我使用jquery創建了動態列表(帶有超鏈接的列表)。當我第一次單擊該鏈接時,它將轉到下一頁。 當我單擊鏈接時,我使用cookie來保存鏈接值的索引。再次運行該應用程序,從onload中的cookie中獲取已保存的索引值。使用該值更改特定鏈接的顏色。 現在,我想再次運行該應用程序,該鏈接以紅色顯示,其他鏈接(未訪問)以藍色顯示。 這個怎么做?

   $(".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;

    }  

請指導我。

提前致謝

使用css:visited標簽。 否則,如果您想通過jquery來執行此操作而沒有任何插件,請在jsfiddle上查看我的實現http://jsfiddle.net/JjMAX/1/

實際上有一個jQuery Visited插件 ,可讓您獲取頁面中的已訪問鏈接。

包含后,您可以選擇鏈接並為課程添加新的顏色:

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

請注意,在這種情況下,您必須在<li>內添加<a> ,因為我認為訪問的功能與錨點上的鏈接沒有嚴格的聯系,而不是單擊列表項。

相反,如果您需要使用cookie選項,請給我一些時間來檢查您的代碼! :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM