作为 javascript 的初学者,有些人对
addEventListener()
功能,而不是使用
variable.onclick
这是我正在测试它的代码
/*
* A simple function to swap the text of two elements
*/
function swapFunction(){
var siteTitle = document.getElementById("site_title");
var siteText = document.getElementById("site_text");
var temp = siteTitle.innerHTML;
siteTitle.innerHTML = siteText.innerHTML;
siteText.innerHTML = temp;
return false;
}
/*
* A function to handle the page load
*/
function fullyLoaded(){
var testEvent = document.getElementById("test_click");
/*
* Why does the line that has been commented out not work
* Yet the line beneath it does?
*/
//testEvent.addEventListener("click", swapFunction(), false);
testEvent.onclick = swapFunction;
}
window.onload = fullyLoaded;
html
<html>
<head>
<title>This is a test application</title>
</head>
<body>
<h1 id="site_title">Welcome To My Site</h1>
<p id="site_text">This is a test site that I am practicing on</p>
<a id="test_click" href="#">test click</a>
</body>
</html>
请帮忙解释IE中事件处理程序和addEventListener()或attachEvent()的概念
提前致谢