我希望有一个表在鼠标悬停时自行更改,并在鼠标输出时更改回原始表。这是我能做的最好的事情:
<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>
但是,有时当我将鼠标移出时,静止内部的内容不会变回原来的内容。有更好的方法吗?
谢谢!
答案 0 :(得分:16)
嗯,你当然可以用CSS - http://jsfiddle.net/32HpH/
来做
div#line1 span#a {
display: inline;
}
div#line1:hover span#a {
display: none;
}
div#line1 span#b {
display: none;
}
div#line1:hover span#b {
display: inline;
}
<div id="line1">
<table border="1">
<tr>
<td>
<span id="a">this is sick</span><span id="b">this is awesome</span>
</td>
</tr>
</table>
</div>
答案 1 :(得分:3)
我会做这样的事情:
<td
onmouseover="this.innerHTML='this is awsome';"
onmouseout="this.innerHTML='this is sick';">
this is sick
</td>
这是JSFiddle。
答案 2 :(得分:1)
好吧,正如你在Question中指定的那样触发onmouseover和onmouseout事件,所以最好使用Javascript。点击Fiddle
<!DOCTYPE html>
<head>
<title>change text on mouse over and change back on mouse out
</title>
<script type="text/javascript">
function changeText(text)
{
var display = document.getElementById('text-display');
display.innerHTML = "";
display.innerHTML = text;
}
function changeback(text)
{
var display = document.getElementById('text-display');
display.innerHTML = "";
display.innerHTML = text;
}
</script>
<style>
#box {
float: left;
width: 150px;
height: 150px;
margin-left: 20px;
margin-top: 20px;
padding: 15px;
border: 5px solid black;
}
</style>
</head>
<html>
<body>
<div id="box" onmouseover="changeText('Yes, this is Onmouseover Text')" onmouseout="changeback('any thing')" >
<div id="text-display" >
any thing
</div>
</div>
</body>
</html>
答案 3 :(得分:0)
CSS only选项,用于防止表列(或其他包含元素)由于悬停时文本更改而动态调整大小。宽度始终是最长文本的长度。
仅限CSS2。
<!doctype html>
<html lang="en">
<head>
<style>
.flip>span {
color: transparent;
float: none;
}
.flip:hover>span {
color: black;
float: left;
}
.flip>span:first-child {
color: black;
float: left;
}
.flip:hover>span:first-child {
color: transparent;
float: none;
}
</style>
</head>
<body>
<table border='1'>
<tr>
<td class="flip">
<span>normal text</span>
<span>hover text</span></td>
<td>col 2</td>
</tr>
<tr>
<td class="flip">
<span>other normal text</span>
<span>other hover text</span></td>
<td>col 2</td>
</tr>
</table>
</body>
</html>