admin管理员组文章数量:1277526
I'm trying to do something, like when you Mouseover on text inside a span, the background changes. My code:
<script>
function unhighlight(x) {
x.style.backgroundColor="transparent"
}
function highlight(x) {
x.style.backgroundColor="red"
}
</script>
<span onmouseover="highlight(this)" onmouseout="unhighlight(this)">
<h2>What's New</h2>
</span>
The reason I don't apply it to the h2 is a little plicated. Don't need to explain. Help?
I'm trying to do something, like when you Mouseover on text inside a span, the background changes. My code:
<script>
function unhighlight(x) {
x.style.backgroundColor="transparent"
}
function highlight(x) {
x.style.backgroundColor="red"
}
</script>
<span onmouseover="highlight(this)" onmouseout="unhighlight(this)">
<h2>What's New</h2>
</span>
The reason I don't apply it to the h2 is a little plicated. Don't need to explain. Help?
Share Improve this question edited Oct 24, 2014 at 9:24 Bojan Petkovski 6,9331 gold badge26 silver badges35 bronze badges asked Oct 24, 2014 at 9:21 Ali BdeirAli Bdeir 4,37512 gold badges61 silver badges128 bronze badges 2-
1
Since no one has mentioned yet, I'd add that
<h2>
is NOT a valid content for<span>
element. In fact the permitted content of<span>
is phrasing content. Therefore you'd better use<div>
element rather than<span>
, probably. – Hashem Qolami Commented Oct 24, 2014 at 9:31 -
If you want to highlight the text itself rather than the whole space, you should wrap the content of
<h2>
by an inline container line<span>
element. Then apply the background color onh2 > span:hover
- Example. – Hashem Qolami Commented Oct 24, 2014 at 9:54
4 Answers
Reset to default 7Your javacript is fine.
The span element is the inline level generic container. It also helps to inform the structure of document, but it is used to group or wrap other inline elements and/or text, rather than block level elements.
So h2
is not valid child for span
:
html standard
function unhighlight(x) {
x.style.backgroundColor = "transparent"
}
function highlight(x) {
x.style.backgroundColor = "red"
}
span {
display: block;
}
<span onmouseover="highlight(this);" onmouseout="unhighlight(this)"><h2>What's New</h2></span>
I suggest for containers to use block elements like div
. And also i suggest to use css for this:
div:hover {
background: red;
}
<div>
<h2>What's New</h2>
</div>
Just set CSS to <span>
display: block;
You need to change your span element to an inline block for this to work :
span {
display: inline-block;
}
But note that you can achieve the same effect with CSS only :
span {
display: inline-block;
background-color: transparent;
}
span:hover {
background-color: red;
}
You don't need javascript to do it, just with HTML and CSS :
#myText {
display: inline-block;
background-color: transparent;
}
#myText:hover {
display: inline-block;
background-color: red;
}
<span id="myText"><h2>What's New</h2></span>
本文标签: javascriptHow can I add a mouseover effect to a spanStack Overflow
版权声明:本文标题:javascript - How can I add a mouseover effect to a span? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741222102a2361159.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论