admin管理员组文章数量:1305657
I have a div
and when I mouse over it I want to change the background (and possibly other properties). I can do it by calling el.style.backcolor = ""
, but is there a way I can add another CSS style to it then remove it later? Like style += mouseOverStyle
and then style -= mouseOverStyle
. That way I could select the properties to change in the CSS instead of in the JavaScript code.
EDIT: I may want to apply the new style in other situations, not just mouseover, so #div:hover isn't really a general solution. What I'm really asking is is there something like style.add("style") and style.remove("style")?
I have a div
and when I mouse over it I want to change the background (and possibly other properties). I can do it by calling el.style.backcolor = ""
, but is there a way I can add another CSS style to it then remove it later? Like style += mouseOverStyle
and then style -= mouseOverStyle
. That way I could select the properties to change in the CSS instead of in the JavaScript code.
EDIT: I may want to apply the new style in other situations, not just mouseover, so #div:hover isn't really a general solution. What I'm really asking is is there something like style.add("style") and style.remove("style")?
Share Improve this question edited Feb 3, 2011 at 9:47 dan gibson asked Feb 3, 2011 at 8:41 dan gibsondan gibson 3,6653 gold badges42 silver badges58 bronze badges6 Answers
Reset to default 6If I understood you correctly, you're asking to change the background when you hover your div element?
This is easily done via CSS, no Javascript or other codes are necessary!
#myDiv
{
background-color: #f00;
}
#myDiv:hover
{
background-color: #00f;
}
Of course you can change other styles too, you don't need to add another class to change one or more styles.
Hope that helps :)
Add or remove CSS classes when needed.
Adding:
yourElement.classList.add('my_class');
Removing:
yourElement.classList.remove('my_class');
See https://developer.mozilla/en-US/docs/Web/API/Element/classList
You could try using multiple :hover pseudo selectors:
div.style1:hover { background: red }
div.style2:hover { background: yellow }
Then use javascript/jquery to switch between html class attributes
I would reendate you to use a JS library, such as jQuery. In jQuery it is simple like that: http://api.jquery./addClass/ together with the mouseover event handling http://api.jquery./mouseover/.
function changeStyle()
{
document.getElementById("elementID").style.color="green";
}
after the .style. you should paste the css attribute you want to edit.
If you're wanting to use js, change the properties on the user event.
onmouseover : el.style.background = "blue";
onmouseout : el.style.background = "red";
If you want to add more properties as time goes along just throw it in a function
function onMouseOverFunction () {
el.style.background = "blue";
el.style.color = "blue";
el.style.font-size= "1em";
}
本文标签: javascriptHow to temporarily add a CSS styleStack Overflow
版权声明:本文标题:javascript - How to temporarily add a CSS style? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741810339a2398756.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论