0
我是JavaScript新手,目前正在學習有事件處理的DOM基礎知識。 這是我的HTML代碼: -使用JavaScript處理事件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-US">
<head>
<title>My Test Program for Event Handler</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>The first captain of the USS Enterprise NCC-1701 was
<a id="t1" href="http://en.wikipedia.org/">My Link!</a>.</p>
<script type="text/javascript" src="example.js"></script>
</body>
</html>
而且,這是我的Jscript: -
var StrayClickCatcher = {
init: function() {
var links = document.getElementsByTagName("a");
if (typeof document.addEventListener != "undefined") {
document.addEventListener("click", StrayClickCatcher.strayClickListener, false);
for (var i = 0; i < links.length; i++) {
links[i].addEventListener("click", StrayClickCatcher.linkClickListener, false);
}
} else if (typeof document.attachEvent != "undefined") {
document.attachEvent("onClick", StrayClickCatcher.strayClickListener);
for (var i = 0; i < links.length; i++) {
links[i].attachEvent("onClick", StrayClickCatcher.linkClickListener);
}
}
},
strayClickListener: function (event) {
alert("Did you mean to click a link? " + "It's that blue, underlined text.");
},
linkClickListener: function (event) {
if (typeof event == "undefined") {
event = window.event;
}
if (typeof event.stopPropagation != "undefined") {
event.stopPropagation();
} else {
event.cancelBubble = true;
}
}
};
我想顯示的提示信息時,用戶不點擊鏈接。但是,它不工作。我錯過了什麼嗎?如果調用
StrayClickCatcher.init();
在你的js代碼年底
爲了上帝的愛,正確縮進代碼。這是一個難以理解的混亂。 – casraf
你從某處調用過StrayClickCatcher.init()嗎? – sedran