0
我試圖用jQuery來計算點擊次數,將它們存儲在一個cookie中,然後將它們回顯出來。存儲jQuery在cookie中單擊計數,然後回顯它。
我在網上發現了一些允許我這樣做的代碼,但我似乎無法使其工作。它創建cookie文件並在其中存儲'0',但不會在點擊鏈接時更新。任何關於這個代碼出現什麼問題的指導都很好。 :
clickCount.js
jQuery(function(){
$("a").click(function{
var cookiename = 'linkcounter';
if($.cookie(cookiename) == null){
$.cookie(cookiename, 0);
}
$.cookie(cookiename, $.cookie(cookiename)+1);
});
});
的index.php
<?php
session_start();
$counter_file = 'counter';
if(!file_exists($counter_file)){
file_put_contents($counter_file, 0);
}
$counts = (int)file_get_contents($counter_file);
file_put_contents($counter_file, $counts++);
// you can use $counts if you want to display it on the page.
?><!DOCTYPE html>
<html>
<head>
<title>Link Click Counter Test</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.cookie.js"></script>
<script type="text/javascript" src="countdetect.js"></script>
</head>
<body>
<a href="http://www.google.com/"></a><br />
<a href="<?php echo $_SERVER['PHP_SELF']; ?>"></a><br />
Link clicks: <?php echo $counts; ?>
</body>
</html>
謝謝,這解決了我的問題。 –