コア 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>