你可以使用的<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