简体   繁体   English

首先调用函数javascript

[英]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. 我想这样做,以便在第一次点击时我调用一个javascript函数,然后在第二次点击我调用另一个函数为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 使用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 请参见http://jsbin.com/uzivuj

First, you mean JavaScript. 首先,你的意思是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. 每次单击按钮时,它将在调用first()second()之间切换。

<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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何在javascript中先调用函数然后再调用第二个函数 - How do i call a function first and then the second in javascript 加载第一个JavaScript函数后,使用getJSON调用它 - Call second JavaScript function with getJSON after first one is loaded (Javascript)递归 function 在第一次调用时有效,但在第二次调用时无效 - (Javascript) Recursive function that works on first call but not on second When Oscillating numbers 仅在创建第一个Javascript函数中创建的表单之后,才调用第二个Javascript函数 - Call second Javascript function only after the form created in the first Javascript Function has been created 每秒调用javascript函数 - Call javascript function every second javascript函数未在第二次调用时定义 - javascript function is not defined on second call 在第二个 function 中调用第一个 function 将调用第一个 function - calling the first function within a second function that will call the first function 不是首先在元素的第二个动画上调用 promise 函数? - Call promise function on second animation of element not first? 递归函数在第一次调用时起作用,但在第二次调用时不起作用 - Recursive function that works on first call but not on second 第一个执行后如何调用第二个 function - How to call a second function after the execution of first
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM