鼠标悬停时更改文本,鼠标移开时更改回来
我想要一个表格,它可以在鼠标悬停时自行更改,并在鼠标移开时变回原始状态。这是我能做的最好的事情:
<html>
<body>
<div id="line1">
<table onmouseover="showMore()" border="1">
<tr>
<td>this is sick</td>
</tr>
</table>
</div>
<script>
function showMore(){
document.getElementById("line1").innerHTML = "<table border='1' onmouseout='showLess()'><tr><td>this is awesome</td></tr></table>";
}
function showLess(){
document.getElementById("line1").innerHTML = "<table border='1' onmouseover='showMore()'><tr><td>this is sick</td></tr></table>";
}
</script>
</body>
</html>
但是,有时当我将鼠标移出时,静止图像中的内容不会变回原始状态。有更好的方法吗?
谢谢!
I want to have a table that changes itself on mouse over and changes back to the original on mouse out. Here the best I can do:
<html>
<body>
<div id="line1">
<table onmouseover="showMore()" border="1">
<tr>
<td>this is sick</td>
</tr>
</table>
</div>
<script>
function showMore(){
document.getElementById("line1").innerHTML = "<table border='1' onmouseout='showLess()'><tr><td>this is awesome</td></tr></table>";
}
function showLess(){
document.getElementById("line1").innerHTML = "<table border='1' onmouseover='showMore()'><tr><td>this is sick</td></tr></table>";
}
</script>
</body>
</html>
However, sometimes when I move the mouse out, the content inside the still doesn't change back to the original. Is there a better way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,你当然可以使用 CSS 来做到这一点 - http://jsfiddle.net/32HpH/
Well, you could certainly do it with CSS - http://jsfiddle.net/32HpH/
我会做这样的事情:
这是一个 JSFiddle。
I would do something like this:
Here's a JSFiddle.
好吧,正如您在问题中指定的那样触发 onmouseover 和 onmouseout 事件,所以最好使用 Javascript。检查这里小提琴
Well, as you specified in Question to trigger onmouseover and onmouseout event so it is better to use Javascript. Check here Fiddle
CSS 唯一选项,可防止表列(或其他包含元素)因悬停时文本更改而动态调整大小。宽度始终是最长文本的长度。
仅限 CSS2。
上面的小提琴: https://jsfiddle.net/punxphil/mckbrscd/
CSS only option that prevents table columns (or other containing element) dynamically resizing due to text changing on hover. Width is always length of longest text.
CSS2 only.
Fiddle of above: https://jsfiddle.net/punxphil/mckbrscd/