在我尝试学习核心 JavaScript 的过程中,我现在正在学习 EventListeners。
我的问题是 EventListener 下面的代码没有在 IE 中被触发(在 Firefox 和 Chrome 中工作得很好)。
有人可以告诉我我做错了什么吗?
我的代码是:
<p>The first captain of the USS Enterprise NCC-1701 was
<a id="wikipedia" href="http://en.wikipedia.org">Christopher Pike</a>.
</p>
<script type="application/javascript">
var link = document.getElementById("wikipedia");
// for firefox and other browsers
if (typeof link.addEventListener != "undefined")
{
link.addEventListener("click", clickListener, false);
}
// IE only
else if (typeof link.attachEvent != "undefined")
{
link.attachEvent("onclick", clickListener);
}
function clickListener()
{
var link = document.getElementById("wikipedia");
link.setAttribute("href", "www.mysite.com/");
open("http://www.mysite.com");
return false;
}
</script>