[英]Noob attempting js and html coding…and failing miserably
Html: HTML:
<!DOCTYPE html>
<html>
<head>
<SCRIPT language="JavaScript" src="mapbody.js"></SCRIPT>
</head>
<body>
<A HREF="javascript:a_message()">Click for a message..</A>
</body>
</html>
mapbody.js: mapbody.js:
function a_message()
{
alert('I came from an external script! Ha, Ha, Ha!!!!');
}
When I pull up the web page and click the link nothing happens. 当我拉起网页并单击链接时,没有任何反应。 Both files are in the same folder. 这两个文件都在同一文件夹中。 What am I missing? 我想念什么?
Several things: 几件事:
language
-attribute in the script
-tag is obsolete. script
-tag中的language
-attribute已过时。 Use type="text/javascript"
instead. 请改用type="text/javascript"
。 onclick
-attribute, not the href
. JavaScript函数调用应该进入onclick
属性,而不是href
。 A proper implementation might look like this: 正确的实现可能如下所示:
<!DOCTYPE html>
<html>
<head>
<title>Is required!</title>
<script type="text/javascript" src="mapbody.js"></script>
</head>
<body>
<a onclick="a_message();" href="#">Click for a message..</a>
</body>
</html>
Also, binding function-calls to an HTML-Element using the onclick
(or any other onXX
-attribute) is old-school. 同样,使用onclick
(或任何其他onXX
)将函数调用绑定到HTML元素是过时的。 Library's like jQuery enable you to use CSS-selectors to bind actions on certain HTML-elements, which allows a full separation of HTML and JavaScript. 类似于jQuery的库,使您能够使用CSS选择器来绑定某些HTML元素上的动作,从而实现HTML和JavaScript的完全分离。
<!DOCTYPE html>
<html>
<head>
<title>...</title>
</head>
<body>
<a href="#" onclick="a_message();">Click for a message..</a>
<script type="text/javascript" src="mapbody.js"></script>
</body>
</html>
Lukas Knuth was faster than me. Lukas Knuth比我快。 :)
Works for me (Firefox 8) : http://jsfiddle.net/FCXxU/ 适用于我(Firefox 8): http : //jsfiddle.net/FCXxU/
Is the URL to your script good? 脚本的网址是否正确?
A simple way to check that is to add an alert('test');
一种简单的检查方法是添加一个alert('test');
at the beginning of mapbody.js (before the function). 在mapbody.js的开头(在函数之前)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.