2014-04-04 320 views
2

我有隱藏的圖像,並顯示它

var sanmigBottle = document.createElement('sanmigBottle'); 
sanmigBottle.id="sanmigBottle"; 
sanmigBottle.width="25%"; 
sanmigBottle.src='sanmigbottle.png'; 
sanmigBottle.onmousedown="showLibraryInfo()" ; 
sanmigBottle.ontouchstart="showLibraryInfo()" ; 
sanmigBottle.align = "middle"; 
sanmigBottle.style= "-webkit-transform: rotate(350.77259795507035deg) translateZ(0px);'"; 

它加載在我的HTML,但我想隱藏它,我希望有一個觸發顯現。當我輸入這個時,

sanmigBottle.style.visibility="hidden"; 

它不起作用。我怎樣才能做到這一點?我對JS很感興趣。

回答

0

您應該使用

sanmigBottle.style.display = 'none'; 
+0

它仍然顯示。 :( – Jeongbebs

+0

你可以讓一個JSFiddle所以我們可以經過? –

0

你必須設置displaynone

sanmigBottle.style.display="none"; 

然後將其設置爲block或重複的事情,使之再次可見:

sanmigBottle.style.display="block"; 
+0

它仍然顯示。:( – Jeongbebs

+0

@MiguelRivera也許你可以設置一個[jsfiddle](http://jsfiddle.net/)?那樣我們就可以理解爲什麼它不管用。 – RononDex

0

首先檢查圖像的狀態:

function showhide() { 
    var img = document.getElementById('someimage'); 
    if (img.style.visibility === 'hidden') { 
     // Currently hidden, make it visible 
     img.style.visibility = "visible"; 
    } else { 
     // Currently visible, make it hidden 
     img.style.visibility = "hidden"; 
    } 
} 

然後應用可見性。

-1

嘗試使用jQuery

//create element 
var sanmigBottle = $('<img id="sanmigBottle" width="25%" src='sanmigbottle.png' onmousedown="showLibraryInfo()" ontouchstart="showLibraryInfo()" align = "middle" style= "-webkit-transform: rotate(350.77259795507035deg) translateZ(0px);"/>'); 
//add to document and store object in context 
var context = sanmigBottle.appendTo($('Document')); 
//hide object 
context.fadeOut(); 

demo in jsfiddle