国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JS傳參及動態修改頁面布局

2019-11-19 16:50:15
字體:
來源:轉載
供稿:網友

一個關于JS傳參及動態修改頁面布局的簡單小例子。

效果圖:

HTML:

<!DOCTYPE HTML><html> <head>  <meta charset="utf-8" />  <title></title>  <link rel="stylesheet" href="css/style.css" rel="external nofollow" /> </head> <body>  <div id="button"><h1>請為下面的DIV設置樣式:</h1><div>點擊設置</div></div>  <div id="set">   <div class="set1"><span>請選擇背景色:</span><span onclick="cl('red')">紅</span><span onclick="cl('yellow')">黃</span><span onclick="cl('blue')">藍</span></div>   <div class="set2"><span>請選擇寬(px):</span><span onclick="wd(200)">200</span><span onclick="wd(300)">300</span><span onclick="wd(400)">400</span></div>   <div class="set2"><span>請選擇高(px):</span><span onclick="hd(200)">200</span><span onclick="hd(300)">300</span><span onclick="hd(400)">400</span></div>   <span class="btn" onclick="reset()">恢復</span>   <span class="btn" onclick="apply()">確定</span>  </div>  <div id="box"></div>  <script type="text/javascript" src="js/js.js"></script> </body></html>

CSS:

/*top:26%; left:40%;*/#box{width:100px; height: 100px; border:4px black solid;margin-top:100px;}#button div{background: red; width:100px; height:40px; float:left; color:white; text-align: center; line-height: 40px; cursor: pointer;}#set{width:20%; height:28%; border:15px rgb(156,148,156) solid; background:white; position: absolute; top:300px; left:300px; box-shadow: 0 0 8px gray; display:none;}#set div{height: 40px; margin: 20px auto;}#set div span{width:45px; height:40px; display:block; float:left; margin-left:10px; color:white; line-height:40px; text-align:center; cursor:pointer;}#set div span:hover{border:1px black solid}#set div span:nth-of-type(1){width:auto; color:black; font:18px/40px "微軟雅黑"; margin-left:12%; border:none; cursor:auto;}  /*:nth-of-type()表示同級同容器下該種類型的第幾個標簽括號內的數字即指第幾個,從1開始*/#set .set1 span:nth-of-type(2){background:rgb(230,46,0); margin-left:5px;}#set .set1 span:nth-of-type(2):hover{background:red}#set .set1 span:nth-of-type(3){background:rgb(239,189,0)}#set .set1 span:nth-of-type(3):hover{background:yellow}#set .set1 span:nth-of-type(4){background:rgb(90,148,239)}#set .set1 span:nth-of-type(4):hover{background:blue}#set .set2 span{border:1px rgb(198,198,198) solid; color:rgb(136,140,143)}#set .set2 span:nth-of-type(1):hover{background:white;}#set .set2 span:nth-of-type(2){background:rgb(239,239,239); margin-left:5px;}#set .set2 span:nth-of-type(3){background:rgb(239,239,239)}#set .set2 span:nth-of-type(4){background:rgb(239,239,239)}#set .set2 span:hover{background:rgb(239,148,0)}#set .btn{width:60px; height:30px; background:#002952; color:white; display:block; float:left; margin-left:10px; margin-top:10px; text-align:center; line-height:30px; cursor:pointer;}#set span:nth-of-type(1){margin-left:32%;}

JS:

onload = function(){           //加載完畢給div添加點擊事件,可以不這么做,像下面的reset / apply一樣建立一個函數并在該div上寫上Onclick點擊事件調用函數即可 var btn = document.getElementById('button').getElementsByTagName('div'); btn[0].onclick = function(){  document.getElementById('set').style.display = 'block'; }}var box = document.getElementById('box');     //建立全局變量獲取DIV對象function wd(x){            //改變寬度 box.style.width = x + 'px';}function hd(x){           //改變高度 box.style.height = x + 'px';}function cl(x){            //改變顏色 box.style.background = x;}function apply(){ document.getElementById('set').style.display = 'none';}function reset(){            //恢復DIV原來的布局 box.style.width = 100 + 'px'; box.style.height = 100 + 'px'; box.style.background = 'white';}

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持武林網!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 浦江县| 台前县| 和顺县| 福建省| 寿阳县| 濮阳县| 瑞昌市| 玉林市| 若尔盖县| 宁强县| 敦化市| 建瓯市| 兰西县| 襄汾县| 靖远县| 商南县| 临澧县| 林西县| 丹凤县| 沂南县| 福海县| 连城县| 茌平县| 永兴县| 华亭县| 东宁县| 邹平县| 绩溪县| 铜陵市| 南召县| 海盐县| 太和县| 成都市| 纳雍县| 岑巩县| 大宁县| 宜春市| 铜梁县| 南和县| 商水县| 绵阳市|