问题描述
好的,所以我有一个链接指向一个网站的例子:
Okay so I have a link that is directed to one website example:
<a href="https://www.google.com">Click me!</a>
所以我想在第10次之后链接到href链接被点击到不同的位置ive done是有缺陷的,因为它只计算从上次重新加载以来链接被点击的次数。ex:
So what I want to chnage the href link after the 10th time that is clicked to a different location what ive done is flawed becase it only counts the number of times the link has been clicked since the last reload ex:
var count = 0;
$(document).ready(function(){
$('a').click(function(){
count++;
if(count > 10){
$('a').attr("href","https://www.yahoo.com");
}
});
});
所以我需要一个计数器来跟踪它被点击的总次数,而不仅仅是页面重新加载。它需要跟踪每个用户的每次点击,因为我不认为cookie会工作我可能是错误的。
So i need a counter that keeps track of the total times its been clicked not just the times after the page reloads. It needs to keep track of every click from every user because so i dont think cookies would work i may be wrong.
推荐答案
需要保留cookie /本地存储中的值以在多个会话之间保留值。您可以使用像这样的库,使Cookie操作更加轻松
You need to preserve the value in a cookie/local storage to retain the value across multiple sessions. You can use a library like jQuery cookie to make the cookie operations easy
例如:
$(document).ready(function () {
$('a').click(function () {
var count = parseInt($.cookie('link-count'), 10) || 0
count++;
if (count > 10) {
$('a').attr("href", "https://www.yahoo.com");
}
$.cookie('link-count', count)
});
});
演示: - 点击链接并刷新页面计数器将保留该值
Demo: Fiddle - click on the link and refresh the page the counter will retain the value
这篇关于点击链接的总次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!