簡體   English   中英

有沒有一種方法可以禁用單擊n href鏈接n天,使用JavaScript中的cookie

[英]Is there a way to disable href link on click for n days use cookies in JavaScript

有沒有一種方法可以使用Cookie在單擊5天后禁用鏈接,然后再次激活,直到單擊然后禁用5天... etc

我已使用它來禁用,但是刷新頁面鏈接再次處於活動狀態。 我當時只是在考慮使用$ _user cookie來解決這個問題。 Google關於如何使用Cookie的很多知識,但似乎無法理解使用它來獲取結果的邏輯

<a href="mylink.html" class="disabled">Download</a>
<a href="anotherlink.html" class="disabled">Delete</a>

<script>
    jQuery(document).ready(function($) {
        $('a.disabled').on('click', function(e) {
            e.preventDefault();
        });
    });
</script>

請按照以下步驟檢查以獲得所需的結果:-

  1. 現在創建一個如下考慮cookie的函數:-

    function onclickLink(event, this1){ if($.cookie('link_disabled') === null || $.cookie('link_disabled')==undefined) { /* do whatever you want with link*/ $.cookie("link_disabled", 1, { expires : 5 });/*setting the cookie for 5 days*/ } else{event.preventDefault();} }

  2. 相應地修改當前事件:

    jQuery(document).ready(function() { $(parent).on('click', 'a.disabled',function(e) { onclickLink(e,$(this)); }); });

演示版

由於chrome不允許在本地設置cookie,因此此小提琴無法使用chrome。 因此,這是服務器上運行的代碼的版本: 鏈接可在所有瀏覽器中使用。 評論任何人是否遇到任何錯誤。

使用的插件是jquery.cookie.js

請按照以下答案來了解有關Cookie和其他內容的更多信息:

  1. https://stackoverflow.com/a/8537083/3190165

  2. https://stackoverflow.com/a/2824037/3190165

暫無
暫無

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

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