你可以使用的<img>
的“风格”属性来实现你在找什么。在样式属性中,您可以设置display:none
,以隐藏<img>
。要使其再次可见,只需点击一个按钮即可将显示改回display:inline
。请尝试以下操作:
<!doctype html>
<!--Web page that displays quotes at the click of a button click. -->
<!--============================================================= -->
<html>
<head>
<title>Project2</title>
</head>
<body>
<div style="text-align:center">
<p>
<h2> <span style="color:red">Favorite Animals</span> </h2>
<img id='slow' src='slowLoris.jpg' alt='Slow Loris' style='width:200px;height:150px;display:none;'>
<img id='sloth' src='sloth.jpg' alt='Sloth' style='width:200px;height:150px;display:none;'>
</p>
<input type="button" value="Slow Loris"
onmouseover="this.style.backgroundColor='red';"
onmouseout="this.style.backgroundColor='white';"
onclick="
document.getElementById('sloth').style='width:200px;height:150px;display:none;'
document.getElementById('slow').style='width:200px;height:150px;display:inline;'
document.getElementById('outputDiv').innerHTML=
'This animal might look like a harmless, big-eyed baby ewok, but ' +
'the slow loris is one of the only venomous mammals in the world. ' +
'Its subtle nature makes it popular in the illegal pet trade, but this ' +
'furry creature also carries a toxin that is released from the brachial ' +
'gland on the sides of its elbows. If threatened, the loris can take ' +
'the toxin into its mouth and mix it with saliva. The animal may also ' +
'lick or rub its hair with this mixture to deter predators from attack. ' +
'The toxin can cause death by anaphylactic shock in some people.';">
<input type="button" value="Sloth"
onmouseover="this.style.backgroundColor='red';"
onmouseout="this.style.backgroundColor='white';"
onclick="
document.getElementById('slow').style='width:200px;height:150px;display:none';
document.getElementById('sloth').style='width:200px;height:150px;display:inline;'
document.getElementById('outputDiv').innerHTML=
'Sloths—the sluggish tree-dwellers of Central and South America—spend ' +
'their lives in the tropical rain forests. They move through the canopy ' +
'at a rate of about 40 yards per day, munching on leaves, twigs and buds. ' +
'Sloths have an exceptionally low metabolic rate and spend 15 to 20 hours ' +
'per day sleeping. And surprisingly enough, the long-armed animals are ' +
'excellent swimmers. They occasionally drop from their treetop perches ' +
'into water for a paddle.'">
</div>
<div id="outputDiv"></div>
</body>
</html>
编辑
如果你不介意改变你的代码位,更清洁的解决方案是:
<!doctype html>
<!--Web page that displays quotes at the click of a button click. -->
<!--============================================================= -->
<html>
<head>
<title>Project2</title>
</head>
<body style='text-align:center'>
<h2> <span style="color:red">Favorite Animals</span> </h2>
<div id='slow' style='text-align:center;display:none'>
<img src='slowLoris.jpg' alt='Slow Loris'>
<p>This animal might look like a harmless, big-eyed baby ewok, but
the slow loris is one of the only venomous mammals in the world.
Its subtle nature makes it popular in the illegal pet trade, but this
furry creature also carries a toxin that is released from the brachial
gland on the sides of its elbows. If threatened, the loris can take
the toxin into its mouth and mix it with saliva. The animal may also
lick or rub its hair with this mixture to deter predators from attack.
The toxin can cause death by anaphylactic shock in some people.</p>
</div>
<div id='sloth' style='text-align:center;display:none'>
<img src='sloth.jpg' alt='Sloth'>
<p> Sloths—the sluggish tree-dwellers of Central and South America—spend
their lives in the tropical rain forests. They move through the canopy
at a rate of about 40 yards per day, munching on leaves, twigs and buds.
sloths have an exceptionally low metabolic rate and spend 15 to 20 hours
per day sleeping. And surprisingly enough, the long-armed animals are
excellent swimmers. They occasionally drop from their treetop perches
into water for a paddle.</p>
</div>
<input type="button" value="Slow Loris"
onmouseover="this.style.backgroundColor='red';"
onmouseout="this.style.backgroundColor='white';"
onclick="
document.getElementById('slow').style='text-align:center;display:inline';
document.getElementById('sloth').style='text-align:center;display:none'">
<input type="button" value="Sloth"
onmouseover="this.style.backgroundColor='red';"
onmouseout="this.style.backgroundColor='white';"
onclick="
document.getElementById('sloth').style='text-align:center;display:inline';
document.getElementById('slow').style='text-align:center;display:none'">
</div>
</body>
</html>
在按钮的点击,你想要的图像和段落在outputDiv插入。是对的吗? – Nil
切尔西你正在看着这个错误的方式。为什么不让那些带有图像和文字的div出现在他们的面前,点击这些按钮就会显示出来。 – Mihailo