简体   繁体   English

使用JavaScript制作随机名称生成器

[英]Using JavaScript to make a random name generator

I'm reading someone else code from a website they posted on reddit that they made to create a random name generator. 我正在读他们在reddit上发布的网站上的其他代码,他们创建了一个随机名称生成器。 I understand most of what's going on except for the select validation. 我了解除了选择验证之外的大部分内容。 Here's the link to the website. 这是该网站的链接。 http://saturdaykid.com/usernames/generator.html . http://saturdaykid.com/usernames/generator.html I'm still very new to JavaScript but this website is giving me a good idea of how JavaScript is used in a website. 我对JavaScript仍然很陌生,但是这个网站让我很清楚JavaScript在网站中的使用方式。

I don't understand what's going on in the if statement. 我不明白if语句中发生了什么。 In the Markup there is no class names for the select elements or option elements that I can see. 在Markup中,我看不到select元素或选项元素的类名。 So basically, I'm not understanding the if statement is if (!selectedAdj) verifying if the value of this element is null? 所以基本上,我不理解if语句if (!selectedAdj)验证此元素的值是否为null? why does it create a className if there is none originally in the markup? 如果标记中最初没有,那么为什么会创建一个className?

if (!selectedAdj) {
        var adj = document.getElementById("adjective");
        adj.className = "notSelected";
        return false;
    } else {
        var adj = document.getElementById("adjective");
        adj.className = "";
    }

Here's the entire function 这是整个功能

// Ensures the user has chosen an adjective and a noun    
var validate = function() {
    var adjectives = document.getElementById("adjective");
    var selectedAdj = adjectives.options[adjectives.selectedIndex].value;
    if (!selectedAdj) {
        var adj = document.getElementById("adjective");
        adj.className = "notSelected";
        return false;
    } else {
        var adj = document.getElementById("adjective");
        adj.className = "";
    }
    var nouns = document.getElementById("noun");
    var selectedNoun = nouns.options[nouns.selectedIndex].value;
    if (!selectedNoun) {
        var noun = document.getElementById("noun");
        noun.className = "notSelected";
        return false;
    } else {
        var noun = document.getElementById("noun");
        noun.className = "";
    }
    generator(selectedAdj, selectedNoun);
};

"if (!selectedAdj)verifying if the value of this element is null?" “if(!selectedAdj)验证此元素的值是否为空?”

-Yes. -是。 When the option : value="" selected, this is nontransitive javascript. 当选项:value =“”被选中时,这是非传递性的javascript。

"why does it create a className if there is none originally in the markup?" “如果标记中最初没有,那么为什么会创建一个className?”

- create -> assign className (see http://saturdaykid.com/usernames/stylin.css for .notSelected CSS selector) to allow transition effects. - create - > assign className(参见http://saturdaykid.com/usernames/stylin.css for .notSelected CSS selector)以允许转换效果。

he wrote the .notSelected class in his css and he's using javascript to assign a className if the (!selectedAdj) is true? 他在他的CSS中写了.notSelected类,如果(!selectedAdj)为真,他正在使用javascript来分配className?

When the person that landed on this page hits "Generate Your Name" but dosent selected anything the "validate" function will first check for "Adjective" and assign this class to it and return false to exit the function itself. 当登陆此页面的人点击“生成您的姓名”但是选择了任何内容时,“验证”功能将首先检查“形容词”并将此类分配给它并返回false以退出该功能本身。 If the "Adjective" is assign the className will be assign "" value, the result of empty assign return the inherit css selector to this object (the ".select" - in stylin.css). 如果“形容词”被赋值,则className将赋值“”,空赋值的结果将继承css选择器返回给该对象(“.select” - 在stylin.css中)。 Same is done to "Noun". “Noun”也是如此。 If both values are selected next will call "generator" function 如果两个值都被选中,则会调用“generator”功能

Here's a better implementation that does almost the same thing (except it validates both fields even if both are still empty), maybe it's a bit easier to understand: 这是一个更好的实现,几乎完全相同(除非它验证两个字段,即使两者都是空的),也许它更容易理解:

var getSelected = function(elementId) {
    // Gets the "elementId" object
    var element = document.getElementById(elementId);
    // Gets it's currently selected value
    var selected = element.options[element.selectedIndex].value;
    if (!selected) {
        // No value selected, set class to .notSelected
        element.className = "notSelected";
        return false;
    }
    // A value was selected, remove class
    element.className = "";
    // this element is valid
    return selected;
}
var validate = function () {
    var adjective = getSelected("adjectives");
    var noun = getSelected("nouns");
    if (adjective && noun) {
        // Both are valid, continue
        generator(adjective, noun);
    }
}

The original function had a lot of repetition. 原来的功能有很多重复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM