开发者

Jquery code, records how many clicks a link has than changes its url

maybe you can help

On my blog i have 开发者_开发技巧a link which is repeated inside a loop, lets say:

<a href="mysite.com/post" target="_blank" class="myclass">My link</a>

Is there a way, maybe a jquery code that counts how many times a visitor clicks on the link with the class "myclass"; lets say after he clicks three times, I can change the target of the link from "mysite.com/post" to something else, another url.

Any ideeas how I can do this?

Ty very much!


<script>
var times_clicked = 0;
$('a.myclass').live('click', function() {
    times_clicked++;
    if (times_clicked > 5) {
        alert('too much man... too much');
        $(this).attr('href', 'http://toomuch.com');
    }
});
</script>


You could do something like

var counter = 0;
$(function() {
   $('.myclass').live('click', function() {
       counter++;
       // point to google after 3 clicks
       if(counter == 3) {
          $(this).attr('href', 'http://google.com');
       }
   });
});

Live Demo: http://jsfiddle.net/YGSE8/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜