JavaScript内联与传统注册
javascript inline vs. traditional registration
我是js新手,我正在尝试掌握内联与传统注册。 代码块 1(内联)工作正常,但代码块 2(传统)工作不正常。谁能阐明我的错误?
<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
<script>
function gethash() {
var name=prompt("Please enter your name","Harry Potter");
var hash = CryptoJS.MD5(name);
alert("Hello, " + name +".'nYour hash is " + hash)
}
</script>
</head>
<body>
<input type="button" onclick="gethash()" value="Get your hash" />
</body>
</html>
这种使用传统注册的尝试不起作用:
<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
<script type="text/javascript">
function gethash() {
var name=prompt("Please enter your name","Harry Potter");
var hash = CryptoJS.MD5(name);
alert("Hello, " + name +".'nYour hash is " + hash)
}
document.getElementById('myname').onclick = gethash;
</script>
</head>
<body>
<input type="button" value="Get your hash" id="myname" />
</body>
</html>
在您尝试使用 onclick
绑定某些内容时没有这样的元素。将该行移动到文件底部。
如果您查看控制台,您应该会看到如下错误:
未捕获的类型错误:无法设置空的属性"onclick"
这应该可以解决它:
<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
</head>
<body>
<input type="button" value="Get your hash" id="myname" />
<script type="text/javascript">
function gethash() {
var name=prompt("Please enter your name","Harry Potter");
var hash = CryptoJS.MD5(name);
alert("Hello, " + name +".'nYour hash is " + hash)
}
document.getElementById('myname').onclick = gethash;
</script>
</body>
</html>
这导致我们找到更好的方法来做到这一点。使用 window.onload
,您可以在加载所有内容后执行 JS 代码。
window.onload = function() {
document.getElementById('myname').onclick = gethash;
};
调用 document.getElementById('myname').onclick = gethash;
时,您尝试将事件绑定到的元素尚不存在。
您需要将代码放在 onload 事件中,如下所示:
<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
<script type="text/javascript">
function gethash() {
var name=prompt("Please enter your name","Harry Potter");
var hash = CryptoJS.MD5(name);
alert("Hello, " + name +".'nYour hash is " + hash)
}
window.onload = function() {
document.getElementById('myname').onclick = gethash;
}
</script>
</head>
<body>
<input type="button" value="Get your hash" id="myname" />
</body>
</html>
使用 onload 并不理想,因为它在触发之前等待所有资源加载。使用 DOMContentLoaded 事件会更好,或者为了跨浏览器兼容性,请查看 jQuery 等库。
附带说明一下,将演示文稿和逻辑分开是一个非常好的主意,因此应避免使用内联事件处理程序。
sachleen 是正确的(下面的代码有效) - 尽管您可以简单地将整个 JavaScript 块移动到 body
标签的末尾:
<html>
<head>
<script src="http://crypto-js.googlecode.com/svn/tags/3.0.2/build/rollups/md5.js"></script>
</head>
<body>
<input type="button" value="Get your hash" id="myname" />
<script type="text/javascript">
function gethash() {
var name=prompt("Please enter your name","Harry Potter");
var hash = CryptoJS.MD5(name);
alert("Hello, " + name +".'nYour hash is " + hash)
}
document.getElementById('myname').onclick = gethash;
</script>
</body>
</html>
相关文章:
- 点击facebook像素跟踪注册(JS/JQUERY代码)
- 当组件准备就绪时,如何在Polymer中动态注册新属性
- 注册OpenLayers事件时,即使使用匿名函数或绑定,JavaScript关闭也会触发循环内的所有内容
- 如何在单击“提交”按钮时为“新建”窗口编写JavaScript,用“确定”和“取消”显示注册信息
- Angularamd在typescript控制器注册中
- 注册组件的非角度和角度版本
- 如何从已注册的事件发送 JavaScript 对象
- 不使用Grunt.initConfig()注册Grunt任务
- 使用谷歌图表将mousedown事件注册为click
- Node JS,传统的数据结构?(如Set等),任何类似Java.util的node
- Stormpath注册页面上的社交登录
- 可以'我无法提交AJAX注册表
- 注册成功后重定向
- 模态上的输入/注册字段不起作用
- 如何在使用Passport.js和Sails.js注册后重定向到特定位置
- 注册后是否可以定义HTML5自定义元素中的生命周期回调
- 如何通过ajax php将图像上传到服务器本地目录,其中html表单haven't任何传统的提交按钮
- 在Firefox插件中注册控制台服务侦听器
- Cucumber+Mongoose给了我Schema has'尚未注册型号
- JavaScript内联与传统注册