Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
147 views
in Technique[技术] by (71.8m points)

javascript - Allow just one click in class JQUERY

I have a link that will load via ajax some content.

My problem is, I don't want to remove the text "Load comments", I just want to not allow more clicks in this class.

<a href="javascript:;" class="showcomments" id="'.$idf.'">Load comments</a>

Jquery

var Progressajax = false;
$(function() {
     $(".showcomments").click(function(){

               if(Progressajax) return;
                   Progressajax = true;

    var element = $(this);
    var id = element.attr("id");

    Progressajax = false;
           alert("ok");
           $(data).hide().prependTo('.varload'+id).fadeIn(1000);
         //$(element).remove();
        $(element).removeAttr("href");
        $(element).removeClass('showcomments');
    }); 
    });

I just want to see OK the first time. How can I remove this class?

$(element).removeClass('showcomments');

This is not working...

http://jsfiddle.net/qsn1tuk1/

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use jQuery's one() function

$(".showcomments").one("click", function() {

http://www.w3schools.com/jquery/event_one.asp

The one() method attaches one or more event handlers for the selected elements, and specifies a function to run when the event occurs.

When using the one() method, the event handler function is only run ONCE for each element.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...