我對JavaScript的知識非常有限,希望有人能夠幫助我!將函數「document.getElementById」更改爲「document.getElementsByClassName」並添加淡入/淡出 - JavaScript
我有下面的代碼,我試圖使代碼工作,而不是類,然後添加淡入/淡出效果這些功能。我已經添加了一個類和ID設置嘗試儘可能清楚我想要做的事情。
<!DOCTYPE html>
<html>
<head>
<style>
.sub1, .sub2 {
float: right;
background-color: blue;
width: 250px;
height: 250px;
display: none;
}
#sub1, #sub2 {
float: right;
background-color: red;
width: 250px;
height: 250px;
display: none;
}
</style>
<script language="javascript" type="text/javascript">
// class
function SetTabsHidden(tabToHide)
{
document.getElementsByClassName('.sub1').style.display = "none";
document.getElementsByClassName('.sub2').style.display = "none";
}
function SetTabsVisible(tabToShow)
{
document.getElementsByClassName('.sub1').style.display = "none";
document.getElementsByClassName('.sub2').style.display = "none";
document.getElementsByClassName(tabToShow).style.display = "block";
}
// id
function SetTabsHidden(tabToHide)
{
document.getElementById('sub1').style.display = "none";
document.getElementById('sub2').style.display = "none";
$(tabToHide).fadeOut("slow");
}
function SetTabsVisible(tabToShow)
{
elements = document.getElementById('sub1').style.display = "none";
elements = document.getElementById('sub2').style.display = "none";
elements = document.getElementById(tabToShow).style.display = "block";
$(tabToShow).fadeIn("slow");
}
</script>
</head>
<body>
<div id="main">
<input type=button name=type value='class1' onclick="SetTabsVisible('.sub1');";>
<input type=button name=type value='class2' onclick="SetTabsVisible('.sub2');";>
<input type=button name=type value='id1' onclick="SetTabsVisible('sub1');";>
<input type=button name=type value='id2' onclick="SetTabsVisible('sub2');";>
<input type=button name=type value='Hide' onclick="SetTabsHidden();";>
<div class="sub1" id="sub1"></div>
<div class="sub2" id="sub2"></div>
</body>
</html>