简体   繁体   English

在另一个元素的鼠标悬停时更改span元素的CSS类

[英]Change the CSS class of a span element on mouseover of another element

I am trying to change the css class of a span element when another element is hovered over. 当另一个元素悬停在上方时,我试图更改span元素的css类。 The get an idea of what I mean. 明白我的意思。 Load up your Facebook and hover over the main newsfeed posts and in the top right the little down-arrow appears. 加载您的Facebook并将鼠标悬停在主要新闻源帖子上,并在右上方显示小向下箭头。

That is the effect that I want to create. 那就是我想要创造的效果。 So far I have: 到目前为止,我有:

<li id="unique" class="message"><a href="javascript:void(0)" onMouseOver="(MC.className='messageControls')" onMouseOut="(MC.className='hidden')" onclick="callTheFunction();"></li>

<span id="MC" class="hidden">hksdjhkjsdhfkjhsdkjh</span>

But this doesn't work. 但这是行不通的。 Any pointers? 有指针吗? I don't want to use any frameworks/libraries such as jQuery. 我不想使用任何框架/库,例如jQuery。

Your on.. event handlers have no concept of what MC is. 您的on..事件处理程序不知道什么是MC Try like this: 尝试这样:

... onMouseOver="document.getElementById('MC').className='messageControls'" ...

This way you "look up" the element with the id attribute whose value is MC , then act on that element. 这样,您可以“查找”具有id属性的元素,其值为MC ,然后对该元素进行操作。 And as a remdinder, elements must have unique IDs. 并且作为remdinder,元素必须具有唯一的ID。

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

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