简体   繁体   English

在鼠标悬停图像上显示div,在鼠标悬停图像上隐藏div:nextSibling和PreviousSibling不起作用

[英]Show div on mouseover image, hide div on mouseout: nextSibling and previousSibling not working

I have the following code: 我有以下代码:

 <div class="outer-div">
     <img onmouseover="showDiv(this)" src="{{ STATIC_URL }}/images/user_male.png">
     <a onmouseout="hideDiv(this)" href="{% me.some_url %}" style="display: block;">
         <div class="inner-block" onmouseout="hideDiv(elem)">
             <h5>{{ me.title }}</h5>
             <p>{{ me.text }}</p>
             <p>about ? ago</p>
         </div>
     </a>
     <div>
            <p>Comments</p>
            <p>Likes</p>
    </div>
</div>



<script>
function showDiv(elem) {
    elem.style.visibility="hidden";
    elem.nextSibling.style.visibility="visibile";
}

function hideDiv(elem) {
    elem.style.visibility="hidden";
    elem.previousSibling.style.visibility="visibile";
}
</script>

the div "inner-block" is positioned so it goes directly above the image when u hover. div的“内部块”的位置使其在悬停时位于图像上方。 So, the idea is to have an onmouseover for the image that pops up the linked "inner-block" div, and then onmouseout on the link that hides the div and shows the image again. 因此,我们的想法是对图像进行onmouseover,以弹出链接的“ inner-block” div,然后在隐藏div并再次显示图像的链接上进行onmouseout。

When I try to use elem.nextSibling, I get the error that elem.nextSibling is undefined, and thus you can't set the visibility. 当我尝试使用elem.nextSibling时,出现了elem.nextSibling未定义的错误,因此您无法设置可见性。 Thanks! 谢谢! Alternately, is there a different way to do this? 或者,还有其他方法可以做到这一点吗? thank you! 谢谢!

Ok, so now I'm getting the error Uncaught ReferenceError: elem is not defined 好的,所以现在我收到错误信息Uncaught ReferenceError: elem is not defined

Remember, always fix the first error that appears. 请记住,请务必解决出现的第一个错误。

Ok the closest I can get is this jsFiddle . 好吧,我能得到的最接近的是这个jsFiddle

Anyone feel free to build upon my answer. 任何人都可以根据我的回答进行构建。

I think you are getting it wrong nextSibling and previousSibling is work on the same parent but your <div class="inner-block" onmouseout="hideDiv(elem)"> has no sibling 我认为您在nextSibling和previousSibling在同一父<div class="inner-block" onmouseout="hideDiv(elem)">上工作时出错,但是您的<div class="inner-block" onmouseout="hideDiv(elem)">没有兄弟姐妹

more detail : http://www.w3schools.com/dom/prop_node_nextsibling.asp 更多详细信息: http : //www.w3schools.com/dom/prop_node_nextsibling.asp

I think you'd better set element id and use getElementById instead of sibling 我认为您最好设置元素ID并使用getElementById而不是同级

Alright well here is my attempt: 好吧,这是我的尝试:

        <div id="outer-div">
        <img id ="img" onmouseover="showDiv()" src="http://www.crunchbase.com/assets/images/resized/0004/1621/41621v6-max-250x250.jpg" style = "visibility:visible;"/>
        <div id="inner-block" onmouseout="hideDiv()" style="visibility:hidden;">
            <a href="http://www.stackoverflow.com" style="display: block;">
                 <div>
                     <h5>{{ me.title }}</h5>
                     <p>{{ me.text }}</p>
                     <p>about ? ago</p>
                 </div>
             </a>
        </div>
         <div>
                <p>Comments</p>
                <p>Likes</p>
        </div>
    </div>

And here is the javascript: 这是javascript:

<script>
    function showDiv() {
        document.getElementById("img").style.visibility = "hidden";
        document.getElementById("inner-block").style.visibility = "visible";
    }

    function hideDiv() {
        document.getElementById("inner-block").style.visibility = "hidden";
        document.getElementById("img").style.visibility = "visible";
    }
</script>

Couple of things: main changes I did was instead of putting the function on the anchor tag, I encapsulated that all into its own div. 几件事情:我所做的主要更改是将函数封装在自己的div中,而不是将函数放在anchor标签上。 I think this is a lot better if you specify a specific area which in this case is the div around it. 我认为如果指定一个特定区域(在这种情况下是其周围的div)会更好。 This makes it better for the future if say you didn't want it to be links but a certain section, etc. 如果说您不希望它成为链接而是某个部分等,则对于将来更好。

I also used document.getElementById. 我还使用了document.getElementById。 I'm pretty sure its possible to do it with previousSibling and nextSibling (as to why there was a undefined error still kinda confuses me so I'd have to play around with it more) but overall I think this is better style anyways. 我很确定可以使用previousSibling和nextSibling(至于为什么仍然存在未定义的错误,这仍然让我感到困惑,所以我不得不多做些事情),但总的来说,我认为这是更好的样式。 Unless you ALWAYS wanted to get the sibling before and the sibling after the element, I would prefer it you do it this way -- specifying which elements you want to disappear and reappear is a lot more organized. 除非您始终希望获得该元素之前的同级元素,否则除非您始终希望获得该元素的同级元素,否则我宁愿使用这种方式-指定要消失并重新出现的元素要更有条理。

Right now the links aren't directly above the image. 现在,链接并不位于图像的正上方。 If you want to do that, I think you could play around with absolute positioning or floats to get it to work but I'll let you try that for yourself. 如果您想这样做,我认为您可以尝试绝对定位或浮动操作以使其正常工作,但我会让您自己尝试一下。 Hope this helps. 希望这可以帮助。

Well, I figured out the problem. 好吧,我发现了问题所在。 elem.nextSibling and elem.previousSibling were both returning text nodes. elem.nextSibling和elem.previousSibling都返回文本节点。

So I ended up using 所以我最终使用

elem.nextSibling.nextSibling

and

elem.previousSibling.previousSibling

Woohoo! oo! always debug- 总是调试-

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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