今天学习了JS和html结合的,点击来切换到不同内容显示。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>信2005-2 赵磊</title>
<script type="text/jscript">
//显示tab(tabHeadId:tab头中当前的超链接;tabContentId要显示的层ID)
function showTab(tabHeadId,tabContentId)
{
//tab层
var tabDiv = document.getElementById("tabDiv");
//将tab层中所有的内容层设为不可见
//遍历tab层下的所有子节点
var taContents = tabDiv.childNodes;
for(i=0; i<taContents.length; i++)
{
//将所有内容层都设为不可见
if(taContents[i].id!=null && taContents[i].id != 'tabsHead')
{
taContents[i].style.display = 'none';
}
}
//将要显示的层设为可见
document.getElementById(tabContentId).style.display = 'block';
//遍历tab头中所有的超链接
var tabHeads = document.getElementById('tabsHead').getElementsByTagName('a');
for(i=0; i<tabHeads.length; i++)
{
//将超链接的样式设为未选的tab头样式
tabHeads[i].className='tabs';
}
//将当前超链接的样式设为已选tab头样式
document.getElementById(tabHeadId).className='curtab';
document.getElementById(tabHeadId).blur();
}
</script>
</head>
<style>
.tabDiv{
border:1px solid #F00;
}
</style>
<body>
<h1 align = "center" >赵磊的个人主页</h1>
<div class = "tabDiv" id = "tabDiv">
<div id="tabsHead">
<a id="tabs1" class="curtab" href="javascript:showTab('tabs1','tabContent1')">基本资料</a> <a id="tabs2" class="tabs" href="javascript:showTab('tabs2','tabContent2')">获奖记录</a>
</div>
<div class = "tabContent1" id = "tabContent1">
<p>1</p>
</div>
<div class = "tabContent2" id = "tabContent2" style = "display:none">
<p>2</p>
</div>
</div>
</body>
</html>
