HTML 表单已显示,但 javascript 回调未执行。 (以及如何合并到一个文件中?)
谢谢
<html>
<head>
<title> Mouse click test</title>
</head>
<body>
<h1>Mouse Click Test</h1>
<p>Click the mouse on the test link below. A message below will indicate which button was clicked.</p>
<h2><a href = "#" id = "testlink">Test Link</a></h2>
<form name = "form1">
<textarea rows = "10" cols = "70" name = "info"></textarea>
</form>
<script type = "text/javascript" scr = "click.js">
</script>
</body>
</html>
<小时/>
function mousestatus(e){
document.write("hello");
if (!e) e = window.event;
btn = e.button;
whichone = (btn < 2) ? "Left" : "Right";
message = e.type + " : " + whichone + "\n";
document.form1.info.value += message;
}
obj = document.getElementById("testlink");
obj.onmousedown = mousestatus;
obj.onmouseup = mousestatus;
obj.onclick = mousestatus;
obj.ondblclick = mousestatus;
最佳答案
<script>
的属性标签是 src
,不是scr
。这:
<script type = "text/javascript" scr = "click.js">
应该是:
<script type="text/javascript" src="click.js">
并摆脱 document.write
调用, document.write
将会:
Writing to a document that has already loaded without calling
document.open()
will automatically perform adocument.open
call.
和 document.open
:
If a document exists in the target, this method clears it.
打电话document.write
除内联内的任何地方 <script>
在插入新内容之前会删除所有内容;大约是您应该使用 document.write
的唯一一次是这样的:
<p>Where is <script type="text/javascript">document.write('pancakes')</script> house?</p>
您通常会使用 console.log('...')
或console.debug('...')
用于快速调试消息(假设您使用带有控制台的浏览器并且打开了控制台)。
一旦你解决了这些问题,你应该有一些比抛出错误(或什么都不抛出)更有趣的东西:http://jsfiddle.net/ambiguous/5kHVy/
您可能想要阅读较新的事件处理接口(interface)(例如 addEventListener
)。
关于javascript - HTML/Javascript 回调未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7061484/