最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTML5: how do I disable all links after any one of them are clicked? - Stack Overflow

programmeradmin1浏览0评论

This is what my current link looks like:

<a href="/board/take_turn?id=313&x=1&y=2" data-remote="true" class="square"> <span class="ttt_square"> &nbsp; </span> </a>

I know it's not ajax because the jQuery ajax:success type events are never invoked.

This is teh site that I'm working on: /

This is what my current link looks like:

<a href="/board/take_turn?id=313&x=1&y=2" data-remote="true" class="square"> <span class="ttt_square"> &nbsp; </span> </a>

I know it's not ajax because the jQuery ajax:success type events are never invoked.

This is teh site that I'm working on: http://ttt-ai.heroku./

Share Improve this question edited Feb 2, 2011 at 16:29 NullVoxPopuli asked Feb 2, 2011 at 3:45 NullVoxPopuliNullVoxPopuli 65.3k76 gold badges214 silver badges361 bronze badges 2
  • "all links" = all links in the page? – gianebao Commented Feb 2, 2011 at 3:49
  • all links after yes is clicked on the page. each of the square links, a.square – NullVoxPopuli Commented Feb 2, 2011 at 16:31
Add a ment  | 

4 Answers 4

Reset to default 3

If you want to disable all links on a page you can use this:

$("a").live('click', function(e) {
    e.preventDefault;
    return false;
});

Or you can target specific links like this:

$("a.disabledLink").live('click', function(e) {
    e.preventDefault;
    return false;
});

<a href="/board/take_turn?id=313&x=1&y=2" data-remote="true" class="disabledLink">
    <span class="ttt_square">&nbsp;</span>
</a>

Try this,

<script type="text/javascript">
    $(function() {
        $('a').click(function() {
           $(this).attr('href', 'javascript:void(0);');
        });
    });
</script>

And if you will add a class to your link for being more specific; let's say

<a class="disableAfterClick" href="/board/take_turn?id=313&x=1&y=2" data-remote="true"> <span class="ttt_square"> &nbsp; </span> </a>

than

<script type="text/javascript">
    $(function() {
        $('a.disableAfterClick').click(function() {
           $(this).attr('href', 'javascript:void(0);');
        });
    });
</script>

You can use e.preventDefault() .

var $a = $("a");

$a.click(function() {
     $a.click(function(e) {
         e.preventDefault();
     }
});

no need to traverse twice, or if you do

$("a").live("click", function() {
     $("a").click(function(e) {
         e.preventDefault();
     }
});
发布评论

评论列表(0)

  1. 暂无评论