首页 > 解决方案 > 如何在 ES6 中编写 javascript 函数

问题描述

我一直在创建一个 Web 应用程序,并使用两个不同的 DIV 将注册页面和登录页面组合在同一个 HTML 文件中。当页面加载时,“登录 Div”是唯一可见的,当您单击注册时使用一些 JavaScript 的“Register Div is Showed”。所以它工作正常,但我想在 ES6 中编写这些函数。我该怎么做?

function Registration()
{
        const element = document.querySelector(".Registration-form"); // to give a class to the button
        const element2=document.querySelector(".login-form");
        var unhide=element.style.display="block";
        var hider=element2.style.display="none";
}
function login()
{
        const element = document.querySelector(".login-form"); // to give a class to the button
        const element2 = document.querySelector(".Registration-form"); // to give a class to the button
        var unhide=element.style.display="block";
        var hider=element2.style.display="none";
}

标签: javascriptecmascript-6

解决方案


通过一些谷歌搜索,我很确定你可以看到,但这里是:

const registration =() => {

        const element = document.querySelector(".Registration-form");
        const element2=document.querySelector(".login-form");

        const unhide=element.style.display="block";
        const  hider=element2.style.display="none";
}
const  login = () => {
        const element = document.querySelector(".login-form"); // to give a class to the button
        const element2 = document.querySelector(".Registration-form"); // to give a class to the button
        const unhide=element.style.display="block";
        const hider=element2.style.display="none";
}

这些类型的函数称为箭头/胖箭头函数。您应该阅读它们,因为在某些情况下它们不能用作意图。


推荐阅读