如何点击其父级

时间:2015-09-22 11:32:31

标签: javascript html css dom

helo,如果你想查看我要查看的内容,我只是想让我的李孩子可以点击父李,http://technologyvs.co.uk 转到客户说的部分

var selector, elems, makeactive,//this is for li one
    childSelector,childElems , makeChildActive;//this is for li two
    
    
    
    selector = ".parent li";
    elems = document.querySelectorAll(selector);
    
    makeactive = function () {
        for (var i = 0; i < elems.length; i++) {
            elems[i].classList.remove('active');
            this.classList.add('active');
        }
    }
    
    for (var i = 0; i < elems.length; i++) {
    
        elems[i].addEventListener("mousedown", makeactive);
    }
    
    
    childSelector = ".child li"
    childElems = document.querySelectorAll(childSelector);
    
    
    makeChildActive = function (){
    
    
    for (var j=0;j<childElems.length;j++){
      
    
        childElems[j].classList.remove('child-active');
        this.classList.add('child-active');  
         
        
    }
    
    }
    
    for(var j=0;  j<childElems.length;j++){
    childElems[j].addEventListener("mousedown",makeChildActive);
      
    }


  
li.active{
    color: red;
    }
    
    
    .text li{
    visibility:visible;
        transition: all 1s ease-in-out;
        
    }
    
    
     li.child-active{
    
    visibility: hidden;
    width: 200px;
    margin-left: 25px;     
    font-size: 25px;
    
    } 
<!Doctype html>
    <html>
    <head>
        <title></title>
        <link rel="stylesheet" href="custom.css" type="text/css">
    </head>
    
    <body>
        <h1>heloo</h1>
    <ul class="parent">
        <li>one</li>
        <li>two</li>
        <li>three</li>
        <li>four</li>
    </ul>
        
    <ul class="child">
        <li>text1</li>    
        <li>text2</li>
        <li>text3</li>
        <li>text4</li>
    </ul>       
    <script src="custom.js"></script>                    
    </body>
    </html>

0 个答案:

没有答案