DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> js 綁定帶參數的事件以及手動觸發事件
js 綁定帶參數的事件以及手動觸發事件
編輯:關於JavaScript     
1.html代碼
復制代碼 代碼如下:
<div>
<div id="tab">
<h2>標題一</h2>
<h2>標題二</h2>
<h2>標題三</h2>
<h2>標題四</h2>
div>
<dl id="tabcon">
<dd>內容一</dd>
<dd>內容二</dd>
<dd>內容三</dd>
<dd>內容四</dd>
</dl>
<div>

2 .js部分
復制代碼 代碼如下:
<script type="text/javascript" defer="defer">
var tab = document.getElementById("tab").getElementsByTagName("h2");
function swap(n) {
return function() {
for(var i=0; i<tab.length; i++) {
document.getElementById("tabcon" + i).style.display = "none";
document.getElementById("tab" + i).className = "";
}
document.getElementById("tabcon" + n).style.display = "block";
document.getElementById("tab" + n).className = "focus";
}
}

for(var i=0; i<tab.length; i++) {
tab[i].setAttribute("id", "tab" + i);
if(window.addEventListener) {
tab[i].addEventListener("mouseover", swap(i), false);
} else if(window.attachEvent) {
tab[i].attachEvent("onmouseover", swap(i));
}
}

var tabcon = document.getElementById("tabcon").getElementsByTagName("dd");
for(i=0; i<tabcon.length; i++) {
tabcon[i].setAttribute("id", "tabcon" + i);
}

if(document.createEvent) {
var evObj = document.createEvent('MouseEvents');
evObj.initEvent( 'mouseover', true, false);
tab[0].dispatchEvent(evObj);
} else if( document.createEventObject) {
tab[0].fireEvent('onmouseover');
}
</script>
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved