下拉菜单的实现classList.add() classList.remove() class属性的添加和删除

DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Titletitle>
    <style>
        .hide{
            display: none;
        }
        .item .header{
            height: 35px;
            background-color: #f25022;
            color: white;
            line-height: 35px;
        }
    style>
head>
<body>
    <div style="height: 48px">div>
    <div style="width: 300px">
        <div class="item">
            <div class="header" onclick="ChangeMenu(this)">菜单1div>
            <div class="content">
                <div>内容1div>
                <div>内容1div>
            div>
        div>
        <div class="item">
            <div  class="header" onclick="ChangeMenu(this)">菜单2div>
            <div class="content hide">
                <div>内容2div>
                <div>内容2div>
            div>
        div>
        <div class="item">
            <div  class="header" onclick="ChangeMenu(this)">菜单3div>
            <div class="content hide">
                <div>内容3div>
                <div>内容3div>
            div>
        div>
        <div class="item">
            <div  class="header" onclick="ChangeMenu(this)">菜单4div>
            <div class="content hide">
                <div>内容4div>
                <div>内容4div>
            div>
        div>
    div>
    <script>
        function ChangeMenu(ths){
            current_head = ths;
            var all_text =current_head.parentElement.parentElement;
            children_text = all_text.children;
            for(var i=0; i<children_text.length; i++){
                current_text = children_text[i];

               current_text.children[1].classList.add('hide')
            }

            current_head.nextElementSibling.classList.remove('hide')



        }
    script>
body>
html>

 

转载于:https://www.cnblogs.com/my-love-is-python/p/9291646.html

你可能感兴趣的:(下拉菜单的实现classList.add() classList.remove() class属性的添加和删除)