如何使用窗口加载函数使我所有的 javascript 都外部化
How do I make all my javascript external with the window onload function?
我正在尝试使我所有的JavaScript外部,包括提交按钮的onclick,我有一个注释掉的window.onload
函数,我无法工作。当我从提交输入中删除onclick="checkInput();"
并在脚本中取消注释window.onload
事件时,我的表单不起作用。
除了不熟悉 JavaScript 之外,有人可以解释我做错了什么.我已经包含一个工作片段,只是不是外部的,感谢您的任何指导。
/*----------------------------------------------
css settings for HTML div exactCenter
------------------------------------------------*/
@import url(http://fonts.googleapis.com/css?family=Raleway);
#main{
width:960px;
margin:50px auto;
font-family:raleway;
}
span{
color:black;
font-weight:bold;
}
h2{
background-color: white;
text-align:center;
border-radius: 10px 10px 0 0;
margin: -10px -40px;
padding: 30px;
}
hr{
border:0;
border-bottom:1px solid blue;
margin: 10px -40px;
margin-bottom: 30px;
}
#form_layout{
width:300px;
float: left;
border-radius: 10px;
font-family:raleway;
border: 2px solid #ccc;
padding: 10px 40px 25px;
margin-top: -2px;
}
input[type=text],input[type=password]{
width:99.5%;
padding: 10px;
margin-top: 8px;
border: 1px solid #ccc;
padding-left: 5px;
font-size: 16px;
font-family:raleway;
}
input[type=submit]{
width: 100%;
background-color:#0467fc;
color: white;
border: 2px solid #0467fc;
padding: 10px;
font-size:20px;
cursor:pointer;
border-radius: 5px;
margin-bottom: 15px;
}
a{
text-decoration:none;
color: cornflowerblue;
}
i{
color: cornflowerblue;
}
p{
font-size:16px;
font-weight:bold;
color:red;
}
<!DOCTYPE html>
<html>
<head>
<script>
//window.on onload functionload = function() {
//document.getElementById('submit').onclick = function(evt) {
//checkInput();
//}//end onload onclick
</script>
</head>
<body>
<div id="main">
<div id="form_layout">
<h2>Palindrome Test</h2>
<br>Enter a 10 character Palindrome.
<!-- Form starts here-->
<form name="pForm" id="pForm" method="post" >
<label>Palindrome:</label>:
<input type="text" name="uput" id="uput"/><br>
<!-- ... all the other stuff ... -->
</form><br>
<input type='submit' id="submit" value="Check Palindrome" onclick="checkInput();"/><br>
<p><span id="eMsg" class="error"></span><p/><br>
</div>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>isPalindrome</title>
<script>
//window.on onload functionload = function() {
//document.getElementById('submit').onclick = function(evt) {
//checkInput();
//}//end window.onload
Palindrome(str);
</script>
</head>
<body>
<div id="main">
<div id="form_layout">
<h2>Palindrome Test</h2>
<br>Enter a 10 character Palindrome.
<!-- Form starts here-->
<form name="pForm" id="pForm" method="post" >
<label>Palindrome:</label>:
<input type="text" name="uput" id="uput"/><br>
<!-- ... all the other stuff ... -->
</form><br>
<input type='submit' id="submit" value="Check Palindrome" onclick="checkInput();"/><br>
<p><span id="eMsg" class="error"></span><p/><br>
</div>
</div>
</body>
</html>
您必须等待 DOM 完成加载,然后才能查询它以查找 DOM 元素和 atach 事件。
-
最简单的解决方法是将
<script>
放在身体的末端。 -
另一个修复是在窗口 onload 事件中附加处理程序(这就是它在下面的代码片段中的工作方式)
在有问题的代码片段中,window.onload的附加方式尚不清楚。同样在脚本表达式的末尾Palindrome(str);
引发错误,因为 var str
未定义,所以我已经修复了它。
请参阅下面的工作片段:
document.getElementById('submit').onclick = function(evt) {
checkInput();
}
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
window.onload = function(){
document.getElementById('submit').onclick = function(evt) {
checkInput();
};};
function checkInput() {
alert('Check input fired');
}//end check input
</script>
</head>
<body>
<div id="main">
<div id="form_layout">
<h2>Palindrome Test</h2>
<br>Enter a 10 character Palindrome.
<!-- Form starts here-->
<form name="pForm" id="pForm" method="post" >
<label>Palindrome:</label>:
<input type="text" name="uput" id="uput"/><br>
<!-- ... all the other stuff ... -->
</form><br>
<input type='submit' id="submit" value="Check Palindrome" /><br>
<p><span id="eMsg" class="error"></span><p/><br>
</div>
</div>
</body>
</html>
相关文章:
- LeadLander formalyze_init.js Javascript外部文件
- 我应该如何用javascript实例化这个日期
- Javascript初始化和错误处理
- 加载成员路由后通过JavaScript初始化
- javascript外部链接文件的可接受数量是多少
- Javascript 外部 API 调用,在 for 循环中使用变量 URL
- 将变量传递给 Javascript 模块化模式方法
- JavaScript模块化和依赖注入之间的区别
- JavaScript初始化函数
- JavaScript初始化函数
- bootstrap选择javascript实例化不创建对象
- javascript标记化器
- 如何使用JavaScript随机化有序列表
- 使用JavaScript随机化html标记
- 我如何在 javascript 中从原型外部化函数
- 如何使用窗口加载函数使我所有的 javascript 都外部化
- 在 Javascript 中外部化字符串
- 如何在外部化脚本时用javascript生成正确的URL
- 为视图外部化JavaScript
- Javascript+jQuery UI:将JSON变量外部化到文件后,如何读回值