Jquery Click Event Not Firing On First Click, but does on second click, why?

前端 未结 4 1127
无人及你
无人及你 2021-02-15 15:20

I\'ve got a jQuery code, which

$("a.reply").click(function() {
//code
});

When I click the link with .reply class the fir

相关标签:
4条回答
  • 2021-02-15 15:34

    This is a longshot, but are you running some sort of tracking script? Like webtrends or coremetrics (or even some of your own script, that's globally looking for all clicks)? I ran into a similar problem a while ago, where the initial-click was being captured by coremetrics. Just a thought.

    0 讨论(0)
  • 2021-02-15 15:41

    I just ran into same problem and I resolved my problem by removing:

    <script src="bootstrap.js"></script>
    

    you can use bootstrap.min.js

    0 讨论(0)
  • 2021-02-15 15:44

    Use Inline CSS for hiding div and use JS/jQuery to show . This way Jquery Click Event will Fire On First Click

         <div class="about-block">
          <div class="title">About us</div>
            <div class="" id="content-text" style="display:none;">
                <p>Show me.</p>
            </div>
        </div>
    
            <script>     
             var x = document.getElementById("content-text");
                 jQuery( '.about-block' ).click(function() {
                   if (x.style.display === "none") {
                      x.style.display = "block";             
    
                    } else {
                      x.style.display = "none";
                    }
                 });
          </script>
    
    0 讨论(0)
  • 2021-02-15 15:50

    Does it still happen if you comment out all your code and simply have an alert("hi") inside the click function?

    Update

    I think Sarfaz has the right idea, but I would use the document ready function like so

    $(document).ready(function(){
      $("a.reply").click(function() {
        //code
      });
    });
    
    0 讨论(0)
提交回复
热议问题