JS傳參及動(dòng)態(tài)修改頁(yè)面布局
一個(gè)關(guān)于JS傳參及動(dòng)態(tài)修改頁(yè)面布局的簡(jiǎn)單小例子。
效果圖:

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>請(qǐng)為下面的DIV設(shè)置樣式:</h1><div>點(diǎn)擊設(shè)置</div></div>
<div id="set">
<div class="set1"><span>請(qǐng)選擇背景色:</span><span onclick="cl('red')">紅</span><span onclick="cl('yellow')">黃</span><span onclick="cl('blue')">藍(lán)</span></div>
<div class="set2"><span>請(qǐng)選擇寬(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>請(qǐng)選擇高(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()">恢復(fù)</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()表示同級(jí)同容器下該種類(lèi)型的第幾個(gè)標(biāo)簽括號(hào)內(nèi)的數(shù)字即指第幾個(gè),從1開(kāi)始*/
#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添加點(diǎn)擊事件,可以不這么做,像下面的reset / apply一樣建立一個(gè)函數(shù)并在該div上寫(xiě)上Onclick點(diǎn)擊事件調(diào)用函數(shù)即可
var btn = document.getElementById('button').getElementsByTagName('div');
btn[0].onclick = function(){
document.getElementById('set').style.display = 'block';
}
}
var box = document.getElementById('box'); //建立全局變量獲取DIV對(duì)象
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(){ //恢復(fù)DIV原來(lái)的布局
box.style.width = 100 + 'px';
box.style.height = 100 + 'px';
box.style.background = 'white';
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
javascript實(shí)現(xiàn)點(diǎn)擊單選按鈕鏈接轉(zhuǎn)向?qū)?yīng)網(wǎng)址的方法
這篇文章主要介紹了javascript實(shí)現(xiàn)點(diǎn)擊單選按鈕鏈接轉(zhuǎn)向?qū)?yīng)網(wǎng)址的方法,涉及javascript鼠標(biāo)事件及頁(yè)面跳轉(zhuǎn)的相關(guān)技巧,簡(jiǎn)單實(shí)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-08-08
jQuery檢測(cè)輸入的字符串包含的中英文的數(shù)量
這篇文章主要介紹了jQuery檢測(cè)輸入的字符串包含的中英文的數(shù)量的實(shí)現(xiàn)方法,非常的實(shí)用,這里推薦給小伙伴,有需要的朋友可以參考下。2015-04-04
JS選項(xiàng)卡動(dòng)態(tài)替換banner圖片路徑的方法
這篇文章主要介紹了JS選項(xiàng)卡動(dòng)態(tài)替換banner圖片路徑的方法,涉及javascript操作文件css樣式的相關(guān)技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-05-05
JS生態(tài)系統(tǒng)加速Tailwind?CSS工作原理探究
這篇文章主要為大家介紹了JS?生態(tài)系統(tǒng)加速Tailwind?CSS使用及工作原理探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
JavaScript代碼實(shí)現(xiàn)圖片循環(huán)滾動(dòng)效果
這篇文章主要介紹了JavaScript代碼實(shí)現(xiàn)圖片循環(huán)滾動(dòng)效果的相關(guān)資料,非常不錯(cuò),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06
JavaScript 選中文字并響應(yīng)獲取的實(shí)現(xiàn)代碼
當(dāng)鼠標(biāo)選擇一段文字時(shí),對(duì)這個(gè)事件產(chǎn)生響應(yīng),并且將選中的文字傳遞出去。2011-08-08
BootstrapValidator超詳細(xì)教程(推薦)
這篇文章主要介紹了BootstrapValidator超詳細(xì)教程(推薦)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-12-12
JS實(shí)現(xiàn)仿百度文庫(kù)評(píng)分功能
本文給大家分享基于js實(shí)現(xiàn)仿百度文庫(kù)評(píng)分功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-01-01
僅IE9/10同時(shí)支持script元素的onload和onreadystatechange事件分析
測(cè)試結(jié)果可以看出,IE9后已經(jīng)開(kāi)始支持script的onload事件了。一直以來(lái)我們判斷js文件是否已經(jīng)加載完成就是用以上的兩個(gè)事件。2011-04-04

