简体   繁体   中英

Show/hide div onmouseover/onmouseout Javascript

I have a hyperlink on my page. I want to show a div when I mouse over the hyperlink, and hide it when I mouseout.

My html:

<a onmouseover="showDiv(this)" onmouseout="hideDiv(this)">
    <img>
    <div class="inner-block">
         Content
    </div>
</a>

Javascript:

function showDiv(elem) {
    elem.getElementsByTagName("div").style.visibility="visible";
}

function hideDiv(elem) {
    elem.getElementsByClassName("inner-block2").style.visibility="hidden";
}

and CSS:

.inner-block {
    visibility: hidden
}

I've tried getElementsByTagName, getElementsByClassName, whatever I try I get

Uncaught TypeError: Cannot set property 'visibility' of undefined

try this :

function showDiv(elem) {
    elem.getElementsByTagName("div")[0].style.visibility="visible";
}

function hideDiv(elem) {
    elem.getElementsByTagName("div")[0].style.visibility = "hidden"
}

since getElementsByClassName("inner-block2") will return NodeList

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