javascript - 按钮 2 应该从按钮 1 开始所有的东西,但是怎么做呢?
问题描述
I 据我所知,我是一个完全的初学者。我所知道的,我已经自学了。我有以下问题。
我有一个通过点击执行的功能。在这个函数中有许多其他的动作,然后执行。现在我想要第二个按钮,它首先再次执行相同的操作。但不想在函数中再次编写所有动作。
肯定有一种方法,当我按下第二个按钮时,按钮一个中的所有内容都会执行?
这是我的按钮 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 中的所有内容。但是如何?
解决方案
你可以把它变成一个命名函数,并在按下按钮时调用它。您也可以尝试将所有内容拆分为更小的函数,这样它们做的事情就会更少。一个函数应该只做一件事情。
推荐阅读
- python - Python关于相等运算符和数组定义的理论
- java - 无法从 CentOS 8 服务器使用 MS JDBC 驱动程序连接到 SQL Server 服务器:SSL 错误但未使用安全连接
- python - 使用 python opencv 检测刻度线
- mysql - 有没有办法在 SQL 中每分钟只选择第一项?
- ignite - 拓扑无效时如何禁用对点燃缓存的所有操作
- kframework - 实施评估上下文列表?
- python - 使用 folium 从数据框制作等值线图
- c - GTK+,基于 C 的软件,图像损坏
- java - 如何防止对象被垃圾收集
- mapbox - 如何在 iOS 上动态更新 Mapbox 的 MGLShapeSource 的 MGLPointFeature 属性值?