2016-11-19 62 views
0

我想有2 按鈕或只是2個標籤或喜歡顯示/隱藏2 imges但彼此獨立。我已經編碼爲1圖像,但它現在不工作,如果我將它設置爲兩次在HTML中。見JSFiddle如何在html中獲取按鈕以顯示/隱藏圖像?

HTML:

<html> 
<head> 
<title></title> 
</head> 
<body> 
<p><img height="600" id="map_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Hsi-h000.png/600px-Hsi-h000.png" style="display: none;" width="600" /> <input id="Mapred" onclick="showImg()" type="submit" value="Mapred" /></p> 

<p><img height="800" id="map_img2" src="https://upload.wikimedia.org/wikipedia/commons/thumb/0/0b/Sandro_Botticelli_-_La_nascita_di_Venere_-_Google_Art_Project_-_edited.jpg/800px-Sandro_Botticelli_-_La_nascita_di_Venere_-_Google_Art_Project_-_edited.jpg" style="display: none;" width="502" /> <input id="Map" onclick="showImg2()"  type="submit" value="Map" /></p> 
</body> 
</html> 
+0

你能分享工作的代碼? –

+1

[Jquery:單擊按鈕顯示/隱藏菜單]的可能重複(http://stackoverflow.com/questions/23322039/jquery-show-hide-menu-with-single-button) –

+0

您應該將其更改爲'樣式.display =「none」;''和'「block;」'否則 –

回答

0

插入id="map_img"src...之間的空間內img標籤 並且在文件名中多餘的空間。

function showImg() { 
 
     document.getElementById("map_img").style.display = ""; 
 
    }
<p> 
 
    <img height="600" id="map_img" src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Hsi-h000.png/600px-Hsi-h000.png" style="display: none;" width="600" /> 
 
    <input id="Map" onclick="showImg()" type="submit" value="Map" /> 
 
    </p>

0

這是你需要什麼?因爲我沒有完全理解你的問題。

function showImg() { 
 
    document.getElementById("map_img").style.display = ""; 
 
} 
 

 
function showImg2() { 
 
    document.getElementById("map_img2").style.display = ""; 
 
}
<p><img height="600" id="map_img"src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Hsi-h000.png/600px-Hsi-h000.png" style="display: none;" width="600" /> 
 

 
<input id="Map" onclick="showImg()" type="submit" value="Map" /></p> 
 

 
<p><img height="600" id="map_img2"src="https://upload.wikimedia.org/wikipedia/commons/thumb/c/c8/Hsi-h000.png/600px-Hsi-h000.png" style="display: none;" width="600" /> 
 

 
<input id="Map" onclick="showImg2()" type="submit" value="Map" /></p>

0

的JavaScript

function showImg() { 
      document.getElementById("map_img").style.display = "block"; 
     } 
function hideImg() { 
     document.getElementById("map_img").style.display = "none"; 
    } 

JQuery的

我見過的jQuery標籤的帖子,我會建議使用Javascript,因爲它的速度更快,不需要加載JQuery,但仍然。

$("#map_img").toggle(); 
$("#map_img").hide(); 
$("#map_img").show(); 
For map_img: 

$(document).ready(function(){ 
    $("#button-link").click(function(event){ 
    //Your actions here 
    }); 
}); 
0

可以簡單地使用

style.display == 'block'
,以阻止任何圖像或使用

style.display == 'none'

我顯示你一個完整的例子這樣就可以聯合國derstand ..

<html> 
 
<body> 
 

 
<h1>What Can JavaScript Do?</h1> 
 
<div id="myDiv"> 
 
\t <img id="myImage" src="pic_bulboff.gif" style="width:100px"> 
 
</div> 
 

 

 
<button onclick="LightOn()">Turn On</button> 
 

 
<button onclick="LightOff()">Turn Off</button> 
 

 
<button id="button" onclick="toggle_visibility('myDiv')">Toggle</button> 
 

 

 
<script type="text/javascript"> 
 
\t function LightOn(){ 
 
\t \t document.getElementById('myImage').src='pic_bulbon.gif' 
 
\t } 
 
\t 
 
\t function LightOff(){ 
 
\t \t document.getElementById('myImage').src='pic_bulboff.gif' 
 
\t } 
 
\t 
 
\t function toggle_visibility(id) { 
 
     var e = document.getElementById(id); 
 
     if(e.style.display == 'block') 
 
      e.style.display = 'none'; 
 
     else 
 
      e.style.display = 'block'; 
 
\t } 
 

 
</script> 
 

 
</body> 
 
</html>

+0

我可以在html中設置兩次嗎? (然後我得到6個按鈕,2個圖像 – htmandvbscript

+0

是的,你可以做到這一點.. –