【问题标题】:Button 2 should start all the things in from button 1, but how?按钮 2 应该从按钮 1 开始所有的东西,但是怎么做呢?
【发布时间】:2021-01-05 15:15:52
【问题描述】:

据我所知,我是一个完全的初学者。我所知道的,我已经自学了。 我有以下问题。

我有一个通过点击执行的功能。在这个函数中有许多其他的动作,然后执行。现在我想要第二个按钮,它首先再次执行相同的操作。但不想在函数中再次编写所有动作。

肯定有一种方法,当我按下第二个按钮时,按钮一个中的所有内容都会执行?

这是我的按钮 1 代码:

const navSextro = document.getElementById("navSextro")
navSextro.addEventListener("click", (ASC) => {

    for (let navbarBottomLogo of navbarBottomLogos) {
        navbarBottomLogo.style.display = "none"
    }

    for (let contactAdress of contactAdresses) {
        contactAdress.style.display ="none"
    }
    for (let btnleistung of btnleistungen) {
        btnleistung.style.display ="none"
    }
    bgStart.classList.remove("headSectionSextro")
    bgStart.classList.remove("headSectionMendel")
    bgStart.classList.remove("headSectionDaven")
    bgStart.classList.remove("headSectionMuenter")
    bgStart.classList.remove("headSectionWald")
    bgStart.classList.remove("headSectionRick")
    bgStart.classList.remove("headSectionIndustrie")

    bgStart.classList.add("headSectionSextro")

    for (let headTextStart of headTextStarts) {
        headTextStart.style.display = "none"
    }
    
    for (sextroBtnLeistung of sextroBtnLeistungen) {
        sextroBtnLeistung.style.display = "flex"
        sextroBtnLeistung.classList.remove("border-bottom-sextro", "border-bottom-mendel", "border-bottom-daven", "border-bottom-rick", "border-bottom-muenter", "border-bottom-wald", "border-bottom-industrie", )
        sextroBtnLeistung.classList.add("border-bottom-sextro")
    }

    let contactAdressSextro = document.getElementById("kontakt-adress-sextro")
    contactAdressSextro.style.display ="block"

    startSextro.style.display = "block"

    navstart.style.display = "flex"
    navlogo.style.display = "flex"

    sectionOne.style.display ="flex"
    sectionTwo.style.display ="flex"
    sectionThree.style.display ="flex"

    navSecBottom.style.display ="flex"

    mapText.style.display ="none"
    philosophie.style.display ="none"
    bewertungen.style.display ="none"

    const navbottom = document.getElementById("navBottom")
    navbottom.style.display ="none"

    const navAboutUs = document.getElementById("navaboutus")
    navAboutUs.style.display ="none"

    //SET BORDER-BOTTOM COLOR

    let borderSectionThree = document.getElementById("kontakt-adress-sextro")
    borderSectionThree.classList.remove("border-bottom-sextro", "border-bottom-mendel", "border-bottom-daven", "border-bottom-rick", "border-bottom-muenter", "border-bottom-wald", "border-bottom-industrie")
    borderSectionThree.classList.add("border-bottom-sextro")
    
    /*STICKY NAVBAR SEXTROSTRASSE*/
    window.onscroll = function() {stickyNavBarSextro()}
    const stickySextro = document.getElementById("navSecBottom").offsetTop

    function stickyNavBarSextro() {
        if(window.pageYOffset > stickySextro) {
            navSecBottom.classList.add("sticky")
        } else {
            navSecBottom.classList.remove("sticky")
        }
    }
    
})

现在我需要第二个按钮的代码来激活按钮 1 的所有功能。但是如何?

【问题讨论】:

  • 创建一个函数,将您的代码从按钮 1 移出,并在您需要的任何地方调用新函数。
  • 嗨@skowng,您可以将按钮1事件侦听器中发生的所有事情提取到外部函数中,这样当您在按钮2上添加点击事件侦听器时,您只需调用相同的函数。

标签: javascript function loops button


【解决方案1】:

你可以把它变成一个命名函数,并在按下按钮时调用它。 您也可以尝试将所有内容拆分为更小的函数,这样它们做的事情就会更少。 一个函数应该只做一件事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 1970-01-01
    • 2013-02-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 2019-11-23
    相关资源
    最近更新 更多