首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript不确定代码的作用

JavaScript不确定代码的作用
EN

Stack Overflow用户
提问于 2018-08-19 11:10:30
回答 2查看 101关注 0票数 0

下面的JavaScript是我在登录脚本中使用的,但我想知道它到底是做什么的。

代码语言:javascript
复制
$(function() {

    $('#login-form-link').click(function(e) {
        $("#login-form").delay(100).fadeIn(100);
        $("#register-form").fadeOut(100);
        $('#register-form-link').removeClass('active');
        $(this).addClass('active');
        e.preventDefault();

    });

    $('#register-form-link').click(function(e) {
        $("#register-form").delay(100).fadeIn(100);
        $("#login-form").fadeOut(100);
        $('#login-form-link').removeClass('active');
        $(this).addClass('active');
        e.preventDefault();
    });

});

我认为这与css有关,可能还与将css类设置为活动有关?

任何帮助都是非常感谢的!

提前感谢!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-08-19 11:20:29

如果您单击登录链接,它将淡出注册表单,同时淡出登录表单,然后它将为登录表单提供活动的CSS类。

如果您单击注册链接,它将淡出登录表单,同时淡出注册表单,然后它将为注册表单提供活动的CSS类。

简而言之,如果您单击log In表单中的log,则会显示。如果单击注册,则会显示注册表单。它也只是做一些动画和css类赋值。

票数 1
EN

Stack Overflow用户

发布于 2018-08-19 11:36:16

代码语言:javascript
复制
// jQuery-way to wait until the document is has loaded
$(function() {

    // jQuery equivalence of "addEventListener" which binds a function 
    // (event-handler) to an element, which triggers when you click on said 
    // element (or children of that element, since events bubbles upwards)
    // You add it once, but it will trigger on EVERY click on the element
    $('#login-form-link').click(function(e) {  

        // jQuery way to show a hidden element with a fade-in animation.
        // Delay is added or else the following fadeOut will start early.
        $("#login-form").delay(100).fadeIn(100);  

        // hide an elment, jquery
        $("#register-form").fadeOut(100); 

        // removes css-class, jquery
        $('#register-form-link').removeClass('active'); 

        // Adds a css-class, jquery
        // "this" in an event-handler refers to the element to which the
        // event-handler was added, so in this case: #login-form-link.
        $(this).addClass('active');

        // Prevents the default browser action for the event. 
        // For example, if the clicked element was a link, say
        // <a href="https://google.com">..</a> this would prevent 
        // the browser from loading up google.
        // If you don't have a href or use "" or "#" the browser might
        // refresh the current page or jump to the top of the page, 
        // in such cases you use the e.preventDefault to prevent that!
        // It's common to use with <a>-tags and it's not jQuery.
        e.preventDefault();

    });

});

这里的"e“是一个MouseEvent (不是jquery)。

除此之外,这对您在jQuery上有很大的提示意义。玩得开心!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51914115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档