相思资源网 Design By www.200059.com
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>
复制代码 代码如下:
<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>
标签:
js,绑定事件
相思资源网 Design By www.200059.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
相思资源网 Design By www.200059.com
暂无js 绑定带参数的事件以及手动触发事件的评论...