javascript - HTML/Javascript 回调未执行

标签 javascript

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 a document.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/

相关文章:

javascript - 单击另一个 div 时将事件类添加到第一个 div

javascript - 如何重复使用浏览器已下载的音频,这样浏览器就不会在使用相同音频文件的其他页面中再次下载该音频?

javascript - 累积 promise 值 "functionally"

javascript - 如何使用行索引而不是使用选择来删除dojo增强网格中的行

javascript - Discord 机器人在玩游戏

javascript - 如何在更改检测后重置 Angular2 中的 ViewContainerRef?

javascript - 仅当所有数组元素都相同时才执行函数

Javascript 或 jQuery 选择器从 `a` 然后使用类到很多 div

javascript - 清除 JavaScript 函数中的 FormData()

javascript - 用javascript计算Wifi RSSI信号距离的方法