javascript - Allow just one click in class JQUERY - Stack Overflow

I have a link that will load via ajax some content.My problem is, I don't want to remove the text

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

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

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

Jquery

var Progressajax = false;
$(function() {
     $(".showments").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('showments');
    }); 
    });
        

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

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

This is not working...

/

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

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

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

Jquery

var Progressajax = false;
$(function() {
     $(".showments").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('showments');
    }); 
    });
        

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

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

This is not working...

http://jsfiddle/qsn1tuk1/

Share Improve this question edited Feb 16, 2022 at 14:28 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Sep 16, 2015 at 18:26 RGSRGS 4,2635 gold badges40 silver badges74 bronze badges 2
  • $('.showments') will find all elements that have that class, and attach click handler to them. if you later remove the class, that doesn't do ANYTHING to the click handler. you'd have to remove the click handler itself, or have the handler check if the element still has the appropriate classs – Marc B Commented Sep 16, 2015 at 18:31
  • It sounds like you want to use $.off() which will turn off the listener on a class. Here is the doc on it. – area28 Commented Sep 16, 2015 at 18:32
Add a ment  | 

2 Answers 2

Reset to default 8

Use jQuery's one() function

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

http://www.w3schools./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.

When you bind an event handler, you bind to the element, not to the class. Removing a class from an element doesn't change which event handlers are bound to the element.

You could use off() to remove the event handler:

$(this).off('click');

http://jsfiddle/om6ggvyu/

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745047141a4608168.html

相关推荐

  • javascript - Allow just one click in class JQUERY - Stack Overflow

    I have a link that will load via ajax some content.My problem is, I don't want to remove the text

    7小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信