为什么我的html脚本都没有工作?

时间:2014-09-20 19:52:09

标签: javascript html

只有函数validateForm脚本在我的html编码中工作。

菜单脚本似乎无法正常工作。

有人可以尝试用我的编码识别这个问题吗?

我无法在外部链接Java,因为我想发布它的Web主机不支持它。 我的代码如下:

<!DOCTYPE html>
<html>
<head>
<title>Our Portolio</title>
<style>img { background:#8A0808;
padding:6px;
border:1px solid #999999; 
}

body {
font: "Trebuchet MS", Verdana, sans-serif;
font-size: 16px;
background-color: dimgrey; 
color: #696969;
padding: 4px;
}

#main {
padding: 5px;
padding-left:  15px;
padding-right: 15px;
background-color: #ffffff;
border-radius: 0 0 5px 5px;
}

h1 {
font: Georgia, serif;
border-bottom: 3px solid #cc9900;
color: #996600;
font-size: 30px;
}

table {
width:100%;
}

table, th , td {
border: 3px solid grey;
border-collapse: collapse;
padding: 5px;
}

th {
text-align: middle; 
}

table tr:nth-child(odd) {
background-color: #f1f1f1;
}
table tr:nth-child(even) {
background-color: #ffffff;
}

ul#menu {
padding: 0;
margin-bottom: 12px;
}

ul#menu li {
display: inline;
margin-right: 3px;
}

ul#menu li a {
background-color: #ffffff;
padding: 10px 20px;
text-decoration: none;
color: #696969;
border-radius: 4px 4px 0 0;
}

ul#menu li a:hover {
color: white;
background-color: black;
}</style>
</head>
<script>document.getElementById("foot01").innerHTML =
"<p>&copy;  " + new Date().getFullYear() + " Copywright 2014 S.S.T.O. All rights         reserved.</p>";

document.getElementById("nav01").innerHTML =
"<ul id='menu'>" +
"<li><a href='index.html'>Home</a></li>" +
"<li><a href='About.html'>About</a></li>" +
"<li><a href='Our_Portfolio.html'>Our Portfolio</a></li>" +
"</ul>";</script>

<script>
function validateForm() {

var x = document.forms["myForm"]["fname"].value;
var y = document.forms["myForm"]["pwd"].value;
//alert(x);
//alert(y);
if (y == null || x == "") {
    alert("All fields must be correctly filled out");
    return false;
}

if (x == "administrator" && y == "14b79d")
{
  document.forms["myform"].submit()
} else
{
  alert("User name and/or password is incorrect");
    return false;
}
}
</script>


<script>document.getElementById("foot01").innerHTML =
"<p>&copy;  " + new Date().getFullYear() + " Copywright 2014 S.S.T.O. All rights    reserved.</p>";

document.getElementById("nav01").innerHTML =
"<ul id='menu'>" +
"<li><a href='index.html'>Home</a></li>" +
"<li><a href='About.html'>About</a></li>" +
"<li><a href='Our_Portfolio.html'>Our Portfolio</a></li>" +
"</ul>";</script>








<body>




<nav id="nav01"></nav>
<div id="main">
<h1>The SSTO's portfolio</h1>
<IMG STYLE="position:absolute; TOP:78px; LEFT:1045px; WIDTH:195px; HEIGHT:65px"    SRC="https://lh6.ggpht.com/EIDo8hYVBfNxwtFxpAvJ0CGtxZENQ- Qctrz5lFn6EaQrp4HKFF_Qr4TwMxKwvaa5Ffbt37I=s170">
<p><strong> If you wish to view our amazing portfolio and receive monthly trading tips:      </strong></p>
<p>Please contact us at:</p>
<p> aryanmisra4@gmail.com</p>
<p>or, dggrishanov@hotmail.com</p>
<p>or, kencmg85@gmail.com</p>
<p>or, elias.kountouris@hotmail.com</p>
<h3>Thank You</h3>
<form name="myForm" action="Answered.html" onsubmit="return validateForm()"   method="post">
Username: <input type="text" name="fname"><br>
Password: <input type="password" name="pwd"><br>
<input type="submit" value="Submit">
</form>
</div>



<div></div>
<footer id="foot01"></footer>


</body>
</html>

1 个答案:

答案 0 :(得分:1)

您的脚本在加载DOM之前运行,并且您尝试访问这些脚本中的DOM元素。

如果您不知道Google是什么DOM。

解决方案是在Document的load事件上运行脚本。

document.onload = function(){
    //Your code here.
    ...
}

validateForm有效,因为您只是定义函数而不是直接访问DOM。