简体   繁体   中英

Call function javascript first second

I want to make it so that on a first click i call one function for javascript, and on a second click i call another function for javascript. How can that be?

Basically i press on this image, it calls this function, if i press it again it calls another function. First then second.

Using html

  <script>
    var clicked = false;

    function doSomething()
   {
      if(clicked)
     {
       alert(2); 

     }
    else
    {
       alert(1); 

    }
   clicked = !clicked;
}
  </script>
</head>
<body>
  <p id="hello" onClick="doSomething();">Hello World</p>
</body>

See exmple here http://jsbin.com/uzivuj

First, you mean JavaScript.

Basically, in the first event handler, you want to remove it, then assign the new one:

function foo()
{
   // Other work
   this.removeEventListner("click", foo, false);
   this.addEventListener("click", bar, false);
}

function bar()
{

}

element.addEventListener("click", foo, false);

This will alternate between calling first() and second() each time the button is clicked.

<input type="button" id="mybutton" value="Click me" />

<script>

    function first() { alert("first"); }
    function second() { alert("second"); }

    var toggleFlag = true;

    $("#mybutton").click(function() {
        toggleFlag ? first() : second();
        toggleFlag = !toggleFlag;
    });

</script>

Hope this help you

<a href=# onclick="onClickEvent()">
    <img src="[Src]" width="208" height="58" >
</a>


var count = 0;

function function1(){
    alert("Function 1");
    count++;
}

function function2(){
     alert("Function 2");
     count = 0;
}

function onClickEvent(){
     if(count ==0){
         function1();
     }else{
         function2();
     }
}

Try This

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#mybutton").click(function(){
            $("p").toggle();
        });`enter code here`
    });
    </script>

    <h2>This is a heading</h2>

    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>

    <input type="button" id="mybutton" value="Click me to show" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM