我的代码有问题.我有两个锚点,点击时显示页面上的不同内容.我还让他们在点击时集中注意力.但是我不知道如何在页面第一次加载时使当前活动的锚集中.
我在网上寻找解决方案,但都没有奏效,所以我希望在这里得到一些帮助!
以下是我的代码:
HTML
<html>
<head>
<link rel="stylesheet" href="style.css">
<script src="script.js"></script>
</head>
<body>
<div class="insandstu">
<h1 class="insandstuh1">More than just books.</h1>
<a class="insanchor" href="javascript:void(0)" onclick="showInstructors();">FOR INSTRUCTORS</a> <span> | </span>
<a class="stuanchor" href="javascript:void(0)" onclick="showStudents();">FOR STUDENTS</a><br>
<!-- FOR INSTRUCTORS -->
<div id="ins-container">
<p class="forins" id="forins1">LMS Integration</p>
<p class="forins" id="forins2">Test banks</p>
<p class="forins" id="forins3">Answer guides</p>
<p class="forins" id="forins4">Powerpoint slides</p>
<a class="forinsanchor" id="forinsanchor" href="">Explore now <img src="./greater-than.svg" alt="greater-than sign" height="12px" style="color: #0dc0dc;"></a>
</div><br>
<!-- FOR STUDENTS -->
<div id="stu-container">
<p class="forstu" id="forstu1">Highlighting</p>
<p class="forstu" id="forstu2">Note-taking</p>
<p class="forstu" id="forstu3">Multiple book formats</p>
<p class="forstu" id="forstu4">Free online</p>
<a class="forstuanchor" id="forstuanchor" href="">Explore now <img src="./greater-than.svg" alt="greater-than sign" height="12px" style="color: #0dc0dc;"></a>
</div>
</div>
</body>
</html>
Css
.hidden {
display: none;
}
.insandstu {
background-color: white;
padding: 40px 80px;
margin-bottom: 20px;
}
.insandstuh1 {
font-size: 30px;
}
.insanchor {
text-decoration: none;
color: #4c7db8;
cursor: pointer;
}
.insanchor:focus {
color: black;
border-bottom: 2px solid black;
}
.stuanchor {
text-decoration: none;
color: #4c7db8;
cursor: pointer;
}
.stuanchor:focus {
color: black;
border-bottom: 2px solid black;
}
/* For Instructor part */
#ins-container {
padding-top: 26px;
}
.forins {
padding: 20px;
font-weight: bold;
display: inline-block;
}
#forins1 {
border-left: 4px solid #0dc0dc;
border-radius: 2px;
width: 120px;
}
#forins2 {
border-left: 4px solid #fdbd3e;
border-radius: 2px;
width: 90px;
}
#forins3 {
border-left: 4px solid #0c9372;
border-radius: 2px;
width: 100px;
}
#forins4 {
border-left: 4px solid #c22032;
border-radius: 2px;
width: 130px;
}
#forinsanchor {
border-left: 4px solid transparent;
border-image: linear-gradient(0deg, #F36B32, #002469, #C02336, #FABC4D, #25C0DA) 4;
border-radius: 2px;
font-weight: bold;
box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);
padding: 25px 50px 20px 20px;
text-decoration: none;
color: #026aa1;
}
/* For Student part */
#stu-container {
display: none;
}
.forstu {
padding: 20px;
font-weight: bold;
display: inline-block;
}
#forstu1 {
border-left: 4px solid #0dc0dc;
border-radius: 2px;
width: 120px;
}
#forstu2 {
border-left: 4px solid #fdbd3e;
border-radius: 2px;
width: 90px;
}
#forstu3 {
border-left: 4px solid #0c9372;
border-radius: 2px;
width: 100px;
padding: 10px 20px;
position: relative;
top: 7.3px;
}
#forstu4 {
border-left: 4px solid #c22032;
border-radius: 2px;
width: 130px;
}
#forstuanchor {
border-left: 4px solid transparent;
border-image: linear-gradient(0deg, #F36B32, #002469, #C02336, #FABC4D, #25C0DA) 4;
border-radius: 2px;
font-weight: bold;
box-shadow: 0 0.2rem 0.4rem rgba(0, 0, 0, 0.2);
padding: 25px 50px 20px 20px;
text-decoration: none;
color: #026aa1;
}
JavaScript
function showInstructors() {
// Get the rows by their id
let instructorsRow = document.getElementById("ins-container");
let studentsRow = document.getElementById("stu-container");
// Remove the hidden class from the instructors row
instructorsRow.classList.remove("hidden");
studentsRow.style.display = "none";
// Add the hidden class to the students row
studentsRow.classList.add("hidden");
}
function showStudents() {
// Get the rows by their id
let instructorsRow = document.getElementById("ins-container");
let studentsRow = document.getElementById("stu-container");
// Remove the hidden class from the students row
studentsRow.classList.remove("hidden");
studentsRow.style.display = "block";
// Add the hidden class to the instructors row
instructorsRow.classList.add("hidden");
}