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

html - Call function javascript first second - Stack Overflow

programmeradmin5浏览0评论

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

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

Share Improve this question edited Nov 24, 2011 at 4:59 soniccool asked Nov 24, 2011 at 4:54 soniccoolsoniccool 6,05823 gold badges63 silver badges101 bronze badges 1
  • It should go from first to second back to first back to second click and so on – soniccool Commented Nov 24, 2011 at 5:03
Add a ment  | 

5 Answers 5

Reset to default 13
  <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./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./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" />
发布评论

评论列表(0)

  1. 暂无评论