作爲在javascript初學者背後的概念,一種是與試圖理解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()的概念
在此先感謝
很像jQuery的執行事件和事件處理像click()例如:onClick是一個單純的別名,或者是實際發生在幕後的快捷方式。 – iGanja