如何在JavaScript中实现下一个和上一个按钮?

发布于 2025-01-22 10:09:00 字数 585 浏览 0 评论 0原文

我在程序中使用了普通的JavaScript和jQuery。我有一个开始按钮,可以让程序自由运行。

<button class="button is-link" id= "start"> Start </button>
document.getElementsByTagName('button')[0].addEventListener('click', function() {
        step1();
        step2();
        step3();
        step4();
        step5();  
    })

function step1() {
   some action
}
function step2() {
   some action
}
function step3() {
   some action
}
function step4() {
   some action
}
function step5() {
   some action
}

我还将如何包含一个下一个和上一个按钮,可以在其中分别显示每个功能并在它们之间来回走动?

I am using plain JavaScript and JQuery in my program. I have a start button that just lets the program run freely.

<button class="button is-link" id= "start"> Start </button>
document.getElementsByTagName('button')[0].addEventListener('click', function() {
        step1();
        step2();
        step3();
        step4();
        step5();  
    })

function step1() {
   some action
}
function step2() {
   some action
}
function step3() {
   some action
}
function step4() {
   some action
}
function step5() {
   some action
}

How would I also include a next and previous button where I can show each function separately and go back and forth between them?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

救星 2025-01-29 10:09:00

首先,您的代码中没有任何jQuery(以这种方式保留):)

其次,如果页面没有重新加载,则应将单个事件侦听器如下附加。

var step = 0;
ele.addEventListener(function() {
// basically store which step you're on and the click adjusts the step
// and then calls the function associated with that step
    step++;
    doNextStep(step);
});

First of all, there isn't any jQuery in your code (keep it that way) :)

Secondly, if the page isn't reloading, then you should attach a single event listener like:

var step = 0;
ele.addEventListener(function() {
// basically store which step you're on and the click adjusts the step
// and then calls the function associated with that step
    step++;
    doNextStep(step);
});
稀香 2025-01-29 10:09:00

简单的开关案例将有所帮助。

<div id="text">Click start to run the app!</div>
<button class="button is-link" id= "start" onclick="start()"> Start </button>
<button class="button is-link" id= "next" onclick="next()"> Next </button>
<button class="button is-link" id= "previous" onclick="previous()"> Previous </button>
<script>
        var step = 0;

        function start() {
            step = 1;
            handleStep();
        }

        function next() {
            step++;
            handleStep();
        }

        function previous() {
            if (step > 0)
                step--;
            handleStep();
        }

        function handleStep() {
            switch (step) {
                case 1:
                    step1();
                    break;
                case 2:
                    step2();
                    break;
                case 3:
                    step3();
                    break;
                case 4:
                    step4();
                    break;
                case 5:
                    step5();
                    break;

                default:
                    step1();
                    // Go to the first step  or any other logic as you want
                    break;
            }
        }


        function setText(text) {
            document.getElementById("text").innerHTML = text;
        }

        function step1() {
            setText("Step 1");
        }
        function step2() {
            setText("Step 2");
        }
        function step3() {
            setText("Step 3");
        }
        function step4() {
            setText("Step 4");
        }
        function step5() {
            setText("Step 5");
        }

    </script>

Simple switch case will be helpful.

<div id="text">Click start to run the app!</div>
<button class="button is-link" id= "start" onclick="start()"> Start </button>
<button class="button is-link" id= "next" onclick="next()"> Next </button>
<button class="button is-link" id= "previous" onclick="previous()"> Previous </button>
<script>
        var step = 0;

        function start() {
            step = 1;
            handleStep();
        }

        function next() {
            step++;
            handleStep();
        }

        function previous() {
            if (step > 0)
                step--;
            handleStep();
        }

        function handleStep() {
            switch (step) {
                case 1:
                    step1();
                    break;
                case 2:
                    step2();
                    break;
                case 3:
                    step3();
                    break;
                case 4:
                    step4();
                    break;
                case 5:
                    step5();
                    break;

                default:
                    step1();
                    // Go to the first step  or any other logic as you want
                    break;
            }
        }


        function setText(text) {
            document.getElementById("text").innerHTML = text;
        }

        function step1() {
            setText("Step 1");
        }
        function step2() {
            setText("Step 2");
        }
        function step3() {
            setText("Step 3");
        }
        function step4() {
            setText("Step 4");
        }
        function step5() {
            setText("Step 5");
        }

    </script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文