简体   繁体   中英

Javascript to get two parents up (no jQuery)

I've got:

<div id='someId'>
    <div>
        <div onclick='someFunction(event)'>
            Text
        </div>
    </div>
</div>

someFunction(event){
    var twoParentsUpId = ??????
}

How can I get this "someId" with the other two DIVs not having any IDs?

... not using jQuery.

This will do it for you https://jsfiddle.net/DIRTY_SMITH/jaer6zzd/

<div id='someId'>
    <div>
        <div onclick='someFunction(event)'>
            Text
        </div>
    </div>
</div>
<script>
function someFunction(event){
    var twoParentsUpId = event.target.parentElement.parentElement;
    twoParentsUpId.style.backgroundColor = "red";
}
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM