創作內容

0 GP

js add&toggle class

作者:鯨魚可可│2018-09-03 09:24:29│巴幣:0│人氣:126
<!DOCTYPE html>
<html>
<head>
<style>
.imgbox {
    float: left;
    text-align: center;
    width: 120px;
    border: 1px solid gray;
    margin: 4px;
    padding: 6px;
}

button {
    width: 100%;
}
.displayNone{
display:none;
}
.vhidden{
visibility:hidden;
}
</style>
</head>
<body>

<h3>Difference between display:none and visiblity: hidden</h3>
<p><strong>visibility:hidden</strong> hides the element, but it still takes up space in the layout.</p>
<p><strong>display:none</strong> removes the element from the document. It does not take up any space.</p>

<div class="imgbox" id="imgbox1">Box 1<br>
  <img src="img_5terre.jpg" alt="Italy" style="width:100%">
  <button onclick="removeElement()">Remove</button>
</div>
<div class="imgbox" id="imgbox2">Box 2<br>
  <img src="img_lights.jpg" alt="Lights" style="width:100%">
  <button onclick="changeVisibility()">Hide</button>
</div>
<div class="imgbox">Box 3<br>
  <img src="img_forest.jpg" alt="Forest" style="width:100%">
  <button onclick="resetElement()">Reset All</button>
</div>

<script>
function removeElement() {
    //document.getElementById("imgbox1").style.display = "none";
    document.getElementById("imgbox1").classList.add("vhidden");
}

function changeVisibility() {
    //document.getElementById("imgbox2").style.visibility = "hidden";
    document.getElementById("imgbox2").classList.add("vhidden");
}

function resetElement() {
    document.getElementById("imgbox1").classList.toggle("vhidden");
    document.getElementById("imgbox2").classList.toggle("vhidden");
       
}
</script>

</body>
</html>

來源:https://www.w3schools.com/css/tryit.asp?filename=trycss_display
引用網址:https://home.gamer.com.tw/TrackBack.php?sn=4117462
All rights reserved. 版權所有,保留一切權利

相關創作

留言共 0 篇留言

我要留言提醒:您尚未登入,請先登入再留言

喜歡★kelvin811215 可決定是否刪除您的留言,請勿發表違反站規文字。

前一篇:react use sa... 後一篇:iframe等比例...

追蹤私訊切換新版閱覽

作品資料夾

wse93035大家
小屋更新黏土人文章!歡迎大家入內澆水OWO看更多我要大聲說昨天21:10


face基於日前微軟官方表示 Internet Explorer 不再支援新的網路標準,可能無法使用新的應用程式來呈現網站內容,在瀏覽器支援度及網站安全性的雙重考量下,為了讓巴友們有更好的使用體驗,巴哈姆特即將於 2019年9月2日 停止支援 Internet Explorer 瀏覽器的頁面呈現和功能。
屆時建議您使用下述瀏覽器來瀏覽巴哈姆特:
。Google Chrome(推薦)
。Mozilla Firefox
。Microsoft Edge(Windows10以上的作業系統版本才可使用)

face我們了解您不想看到廣告的心情⋯ 若您願意支持巴哈姆特永續經營,請將 gamer.com.tw 加入廣告阻擋工具的白名單中,謝謝 !【教學】