有没有办法禁用href链接点击n天使用Cookie中的JavaScript [英] Is there a way to disable href link on click for n days use cookies in JavaScript
问题描述
有一种方法可以使用Cookie在点击5天后停用链接,并再次启用,直到点击后再停用5天... etc
Is there a way to use cookies to disable a link after click for 5 days and be active again until click then disable for 5 days ...etc
我有用于禁用,但刷新页面链接再次活动。我只是想使用$ _user cookie我可能能够做到这一点我的问题是把事情在一起。 Google很多关于如何使用Cookie,但是似乎没有得到使用它来获得我的结果的逻辑。
I have use this for disabling but refreshing page link is active again. I was just thinking using $_user cookie I might be able doing this my problem is getting things together. Google a lot on how to use cookies but can't seem to get the logic of using it to get my results
<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>
推荐答案
按照以下步骤进行检查, : -
Follow these step to check to obtain the result you desire:-
-
现在创建一个函数,如下所示: -
Now a create function as follow to take in account of the cookie:-
function onclickLink(event,this1){
if($。cookie('link_disabled')=== null || $ .cookie('link_disabled')= = undefined){
/ *做任何你想要的链接* /
$ .cookie(link_disabled,1,{expires:5}); / *设置cookie为5天* /
}
else {event.preventDefault();}
}
因此: -
jQuery(document).ready(function(){
$(parent) 'a.disabled',function(e){
onclickLink(e,$(this));
});
});
Demo
这个小提琴不能使用chrome,因为chrome不允许在本地设置cookie。因此,以下是在服务器上运行的代码版本:链接它适用于所有浏览器。评论,如果有人面临任何错误。
This fiddle will not work chrome as chrome does not allow to set cookie locally. So here is the version of the code that is running on the server: link it works in all browsers. Comment if any one faces any bug.
使用的插件是 jquery.cookie.js
The plugin used is jquery.cookie.js
按照以下答案可了解更多关于Cookie和其他内容的信息:
Follow these answers to know more about cookies and other stuff:
http://stackoverflow.com / a / 2824037/3190165
这篇关于有没有办法禁用href链接点击n天使用Cookie中的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!