我的代码有问题.我有两个锚点,点击时显示页面上的不同内容.我还让他们在点击时集中注意力.但是我不知道如何在页面第一次加载时使当前活动的锚集中.

我在网上寻找解决方案,但都没有奏效,所以我希望在这里得到一些帮助!

以下是我的代码:

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");
  }

推荐答案

如果你想聚焦元素,你可以在你的JavaScript函数中使用focus方法:https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/focus.

但您还应该注意到,您的代码中可能存在一些可访问性问题:

  1. 对于这种用例,您应该更喜欢使用按钮,而不是锚.锚应该是为链接.
  2. 看起来您在这里真正构建的是选项卡.在这种情况下,考虑一下https://daily-dev-tips.com/posts/a11y-101-tabs

Javascript相关问答推荐

如何将拖放功能添加到我已自定义为图像的文件输入HTML标签中?

我应该在redux reducer中调用其他reducer函数吗?

如何解决CORS政策的问题

MongoDB中的引用

JQuery. show()工作,但. hide()不工作

角色 map 集/spritebook动画,用户输入不停止在键上相位器3

在服务器上放置了Create Reaction App Build之后的空白页面

MathJax可以导入本地HTML文档使用的JS文件吗?

XSLT处理器未运行

加载背景图像时同步旋转不显示的问题

变量的值在Reaction组件中的Try-Catch语句之外丢失

根据一个条件,如何从处理过的数组中移除一项并将其移动到另一个数组?

图表4-堆叠线和条形图之间的填充区域

如何根据输入数量正确显示alert ?

谷歌饼图3D切片

在高位图中显示每个y轴系列的多个值

JSON Web令牌(JWT)错误:RSA密钥对的签名无效

在AgGrid中显示分组行的单元格值

如何在函数组件中保留对计时器的引用

Playwright:ReferenceError:browserContext未定义