我在最后一个学期之前参加了一个JavaScript课程,现在通过了它 我正在暑假。我们使用的书是JavaScript第8版,Tom Negrino, 多里史密斯。有一个名称生成器的代码。所需的一切都在书中 (p.301)它需要名字和姓氏,但我想添加一个中间名 输出另一个愚蠢的名字。这不是家庭作业,只是一些东西 我想试试。如果您将名字或姓氏留空,则会得到“那就是 不是一个有效的名字“消息。所以我编写了中间名,输入了26个单词 中间名,但代码除了第一个之外不识别任何东西 和姓氏。对我来说,它看起来应该可行,但仍然只是第一次 和姓氏工作。也许另一双眼睛可以帮助我看到什么 我错过了或不包括在内。
感谢任何建议,CK
The HTML
<!DOCTYPE html>
<html>
<head>
<title>Silly Name Generator</title>
<script src="script05.js"></script>
</head>
<body>
<h1>What's your silly name?</h1>
<table>
<tr>
<td class="rtAlign">First Name:</td>
<td><input type="text" id="fName" size="30"></td>
</tr>
<tr>
<td class="rtAlign">Middle Name:</td>
<td><input type="text" id="mName" size="30"></td>
</tr>
<tr>
<td class="rtAlign">Last Name:</td>
<td><input type="text" id="lName" size="30"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="Submit" id="sillySubmit">
</tr>
</table>
<p id="msgField"> </p>
</body>
</html>
JavaScript
window.onload = initAll;
function initAll() {
document.getElementById("sillySubmit").onclick = function() {
document.getElementById("msgField").innerHTML = getSillyName();
return false;
}
}
function getSillyName() {
var firstName = ["Runny", "Buttercup", "Dinky", "Stinky", "Crusty",
"Greasy","Gidget", "Cheesypoof", "Lumpy", "Wacky", "Tiny", "Flunky",
"Fluffy", "Zippy", "Doofus", "Gobsmacked", "Slimy", "Grimy", "Salamander",
"Oily", "Burrito", "Bumpy", "Loopy", "Snotty", "Irving", "Egbert"];
var middleName =["Waffer", "Lilly","Rugrat","Sand", "Fuzzy","Kitty",
"Puppy", "Snuggles","Rubber", "Stinky", "Lulu", "Lala", "Sparkle", "Glitter",
"Silver", "Golden", "Rainbow", "Cloud", "Rain", "Stormy", "Wink", "Sugar",
"Twinkle", "Star", "Halo", "Angel"];
var lastName1 = ["Snicker", "Buffalo", "Gross", "Bubble", "Sheep",
"Corset", "Toilet", "Lizard", "Waffle", "Kumquat", "Burger", "Chimp", "Liver",
"Gorilla", "Rhino", "Emu", "Pizza", "Toad", "Gerbil", "Pickle", "Tofu",
"Chicken", "Potato", "Hamster", "Lemur", "Vermin"];
var lastName2 = ["face", "dip", "nose", "brain", "head", "breath",
"pants", "shorts", "lips", "mouth", "muffin", "butt", "bottom", "elbow",
"honker", "toes", "buns", "spew", "kisser", "fanny", "squirt", "chunks",
"brains", "wit", "juice", "shower"];
var firstNm = document.getElementById("fName").value.toUpperCase();
var middleNm = document.getElementById("mName").value.toUpperCase();
var lastNm = document.getElementById("lName").value.toUpperCase();
var validName = true;
if (firstNm == "") {
validName = false;
}
else {
var firstNum = firstNm.charCodeAt(0) - 65;
if (firstNum < 0 || firstNum > 25) {
validName = false;
}
}
if (!validName) {
document.getElementById("fName").focus();
document.getElementById("fName").select();
return "That's not a valid first name";
}
if (middleNm == "") {
validName = false;
}
else {
var middleNum = middleNm.charCodeAt(0) - 65;
if (middleNum < 0 || middleNum > 25) {
validName = false;
}
}
if (!validName) {
document.getElementById("mName").focus();
document.getElementById("mName").select();
return "That's not a valid middle name";
}
if (lastNm == "") {
validName = false;
}
else {
var lastNum1 = lastNm.charCodeAt(0) - 65;
var lastNum2 = lastNm.charCodeAt((lastNm.length-1)) - 65;
if (lastNum1 < 0 || lastNum1 > 25 || lastNum2 < 0 || lastNum2 > 25) {
validName = false;
}
}
if (!validName) {
document.getElementById("lName").focus();
document.getElementById("lName").select();
return "That's not a valid last name";
}
return "Your silly name is " + firstName[firstNum]
+ " " + middleName[middleNum] + " " + lastName1[lastNum1]
+ lastName2[lastNum2];
}
答案 0 :(得分:1)
您的HTML和JavaScript对我有用。我的猜测是你的浏览器正在缓存旧的JavaScript文件。尝试清除浏览器的缓存或刷新而不缓存(通常是ctrl-f5或shift-f5)。