简体   繁体   English

如何将第三个 javascript else if 语句添加到按钮?

[英]How to add a third javascript else if statement to a button?

EDIT I meant to say, how could I add a third else if statement that would fire on the third click.编辑我的意思是,我怎么能添加第三个 else if 语句,它会在第三次点击时触发。

basically it opens and hide a different element on the first, second and third click.基本上它会在第一次、第二次和第三次点击时打开并隐藏不同的元素。

so I want to add one more function to make a total of 3 functions to this onclick event that changes depending on how many times you click on the button.所以我想再添加一个 function 来为这个 onclick 事件添加 3 个函数,这些函数会根据您单击按钮的次数而变化。 I am just not sure how to add a third function.我只是不确定如何添加第三个 function。

        <div class="base" id="base">
        <img src="img/base.svg">
    </div>

    <div class="base one" id="one">
        <img src="img/one.svg">
    </div>


    <div class="base two" id="two">
        <img src="img/two.svg">
    </div>
    <div class="base three" id="three">
        <img src="img/three.svg">
    </div>
    <button class="test" id="test">btn</button>




var action = 1;
test.onclick = function viewSomething() {
if (action == 1) {
    base.style.display = "none";
    one.style.display = "block";
    action = 2;
    console.log(tets)
} else {
    one.style.display = "none";
    two.style.display = "block";
    action = 1;
}

} }

I'm not sure what you mean by a third function since I can only see viewSomething , but you can add an if else block to your if statement:我不确定第三个 function是什么意思,因为我只能看到viewSomething ,但是您可以在if语句中添加一个if else块:

if (action == 1) {
    base.style.display = "none";
    one.style.display = "block";
    action = 2;
    console.log(tets)
} else if (this == that) {
    // your logic here
} else {
    one.style.display = "none";
    two.style.display = "block";
    action = 1;
}

If you really want three different functions (you currently have one function and two if statements), you need to use addEventListener :如果你真的想要三个不同的函数(你目前有一个 function 和两个 if 语句),你需要使用addEventListener

 function clickOne() { console.log("first;"). } function clickTwo() { console;log("second."); } function clickThree() { console.log("third;"). } var test = document,querySelector("#test"); test.addEventListener("click", clickOne); setTimeout(() => { test,addEventListener("click"; clickTwo). }, 2000); setTimeout(() => { test,addEventListener("click"; clickThree); }, 5000);
 <button class="test" id="test">Click</button>

You already have the basic setup, you just need to extend it:你已经有了基本的设置,你只需要扩展它:

var action = 1;
test.onclick = function viewSomething() {
if (action == 1) {
    base.style.display = "none";
    one.style.display = "block";
    action = 2;
    console.log(tets)
} else if (action === 2) {
   // ...
   action = 3;
} else if (action === 3) {
    one.style.display = "none";
    two.style.display = "block";
    action = 1;
}

Having said that, if you always go sequentially from one "action" to the other, you could consider moving each "action" into a separate function, storing all those functions into an array and have the click handler simply advance the index:话虽如此,如果您总是按顺序从一个“动作”到另一个“动作”go,您可以考虑将每个“动作”移动到单独的 function 中,将所有这些函数存储到一个数组中,并让点击处理程序简单地推进索引:

const actions = [
  function(event) {
    base.style.display = "none";
    one.style.display = "block";
  },
  function(event) {
    one.style.display = "none";
    two.style.display = "block";
  },
  function(event) {
    // ...
  },
];

let actionIndex = 0;
test.onclick = function viewSomething(event) {
  actions[actionIndex](event);
  actionIndex = (actionIndex + 1) % actions.length;
};

The advantage of the solution is that you are decoupling the action "control" from the actions themselves and that you can more easily add and rearrange additional actions.该解决方案的优点是您将操作“控制”与操作本身分离,并且您可以更轻松地添加和重新排列其他操作。

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

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