【发布时间】:2019-08-02 09:45:17
【问题描述】:
我有一个涉及 css 和 javascript 的随机名称生成器代码。按照现在的代码,我可以输入任何名字(不分性别),点击生成按钮,然后会弹出一个随机的名字和姓氏。唯一的问题是,我希望能够为男性、女性和中性名称创建单独的按钮。
我尝试为姓名和女性姓名创建单独的 div id,复制 javascript 并将代码部分更改为“femalename”和“malename”,但这破坏了格式并使其如此,当我按下其中一个生成按钮,两个 div 都会生成一个名称。
<!DOCTYPE HTML>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>Random name generator</title>
<meta charset='utf-8' />
<style type='text/css'>
#name {
color : #444;
font : bold 51px times, Times New Roman, times-roman, georgia, serif;
letter-spacing : -2px;
line-height : 44px;
text-align : center;
text-transform: uppercase;
}
#refresh {
font : normal 11px Gill Sans, Verdana;
letter-spacing : 2px;
line-height : 14px;
text-align : center;
text-transform : uppercase;
}
a {
color : #666;
}
a:hover {
color : #999;
}
</style>
</head>
<body>
<script type='text/javascript'>
first = ['abbie ', 'abby ', 'abu ', 'alec ', 'alek ', 'aleksander ', 'alex ', 'alexander ', 'aaron ', 'adam ', 'andrew ', 'anthony ', 'archer ', 'arthur ', 'austin '];
last = ['williamson', 'davidson', 'edwards', 'ingram', 'olsen'];
name = "";
length = Math.floor(Math.random()) + 1;
for (i = 0; i < length; i++)
name += (first[Math.floor(Math.random()*first.length)]
+ last[Math.floor(Math.random()*last.length)]);
name = name.charAt(0) + name.slice(1);
document.write("<p id='name'>" + name + "</p>");
</script>
<p id="refresh">
<a href='#' onclick='window.location.reload()'>generate a new one</a>
</p>
</body>
</html>
【问题讨论】:
-
你需要两个数组;一个用于女性名字,一个用于男性名字。最好的办法是手动制作这些,因为您需要一个极其复杂的深度学习算法来计算出什么是男性名字和什么是女性名字。
-
你怎么知道一个名字是男是女?
-
谁来选择名字是男是女?用户点击
generate a new one时是否选择了这个? -
@yalda 我想创建两个单独的按钮(一个写着“生成女性名字”,一个写着“生成男性名字”),而不是只有一个写着“生成新名字”的按钮。 "
标签: javascript html css