我对 JavaScript 的了解非常有限,我希望有人能够帮助我!
我有下面的代码,我试图使代码与 class 而不是 id 一起工作,并在这些函数上添加淡入/淡出效果。我添加了一个类和 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>