JavaScript dom操作程式碼例項
分享一段程式碼例項,它實現了對dom操作的簡單演示功能。
程式碼例項如下:
[HTML] 純文字檢視 複製程式碼執行程式碼<!doctype html> <html> <head> <meta charset="utf-8"> <meta name="author" content="http://www.softwhy.com/" /> <title>螞蟻部落</title> <style type="text/css"> h1 { float: left; margin: 0; } h2 { height: 45px; } #div1 { width: 120px; height: 50px; background-color: red; text-align: center; line-height: 50px; position: relative; margin: 0px 0 0 400px; } #jump { position: fixed; margin: 100px 0 0 600px; padding: 20px; background-color: #fefefe; width: 400px; height: 260px; border: 30px solid #8e8e8e; display: none; z-index: 40; } .bg { width: 50px; height: 50px; background-color: #e1e1e3; border: 1px solid #c6c6c6; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } ul { list-style: none; } #submit, #reset { float: left; margin-left: 20px; width: 70px; height: 40px; border: 1px solid; text-align: center; line-height: 40px; background-color: #03275b; color: #fefefe; } #sub { position: absolute; margin: 0 0 0 95px; } .selects { float: right; margin-right: 20px; } #select0 { background-color: #bf2424; color: #fefefe; width: 50px; height: 50px; border: 1px solid #c6c6c6; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } #select1 { background-color: #fbbf00; color: #fefefe; width: 50px; height: 50px; border: 1px solid #c6c6c6; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } #select2 { background-color: #588cfa; color: #fefefe; width: 50px; height: 50px; border: 1px solid #c6c6c6; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } #selectTog0, #selectTog1, #selectTog2 { background-color: #605e5e; color: #fefefe; width: 50px; height: 50px; border: 1px solid #0d0d0d; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } .title { margin-left: 20px; } #transform { border: 5px dashed #000000; height: 300px; width: 300px; background-color: #fefefe; float: left; margin-top: 20px; } .bgToggle { width: 50px; height: 50px; background-color: #605e5e; border: 1px solid #0d0d0d; text-align: center; line-height: 50px; float: left; margin: 0 0 10px 3px; position: relative; } </style> <script type="text/javascript"> window.onload = function() { var oDiv1 = document.getElementById('div1'); var oDiv2 = document.getElementById('jump'); var res = document.getElementById('reset'); var sub = document.getElementById('submit'); var trans2 = document.getElementById('transform'); res.onclick = function() { trans2.style.border = "5px dashed #000000"; trans2.style.height = "300px"; trans2.style.width = "300px"; trans2.style.backgroundColor = "#fefefe"; trans2.style.float = "left"; trans2.style.marginTop = "20px"; } sub.onclick = function() { oDiv2.style.display = "none"; } oDiv1.onclick = function() { oDiv2.style.display = 'block'; newLi(); } } function newLi() { for (var i = 0; i < 9; i++) { setLi(i); } } function setLi(thisLi) { var currLi = "select" + thisLi; document.getElementById(currLi).onmouseover = toggleColor; document.getElementById(currLi).onmouseout = changed; document.getElementById(currLi).onclick = select; } function toggleColor(evt) { if (evt) { var thisLi = evt.target; } else { var thisLi = window.event.srcElement; } if (thisLi.className == "bg") { thisLi.className = "bgToggle"; } else if (thisLi.id == "select0") { thisLi.id = "selectTog0"; } else if (thisLi.id == "select1") { thisLi.id = "selectTog1"; } else if (thisLi.id == "select2") { thisLi.id = "selectTog2"; } else { thisLi.className = "bgToggle"; } } function changed(evt) { if (evt) { var thisLi = evt.target; } else { var thisLi = window.event.srcElement; } if (thisLi.id == "selectTog0") { thisLi.id = "select0"; } else if (thisLi.id == "selectTog1") { thisLi.id = "select1"; } else if (thisLi.id == "selectTog2") { thisLi.id = "select2"; } else { thisLi.className = "bg"; } } function select(evt) { if (evt) { var thisLi = evt.target; } else { var thisLi = window.event.srcElement; } var trans = document.getElementById('transform'); if (thisLi.parentNode.id == "ul2") { trans.style.width = thisLi.value; } if (thisLi.parentNode.id == "ul3") { trans.style.height = thisLi.value; } if (thisLi.id == "selectTog0") { trans.style.backgroundColor = "#bf2424"; } if (thisLi.id == "selectTog1") { trans.style.backgroundColor = "#fbbf00"; } if (thisLi.id == "selectTog2") { trans.style.backgroundColor = "#588cfa"; } } </script> </head> <body> <h1>請為下面的DIV設定樣式:</h1> <div id="div1" tltle="點選設定">點選設定</div> <div id="transform"></div> <div id="jump"> <div class="selects"> <ul id="ul1"> <li id="select0">紅</li> <li id="select1">黃</li> <li id="select2">藍</li> </ul> <ul id="ul2"> <li class="bg" id="select3" value="200">200</li> <li class="bg" id="select4" value="300">300</li> <li class="bg" id="select5" value="400">400</li> </ul> <ul id="ul3"> <li class="bg" id="select6" value="200">200</li> <li class="bg" id="select7" value="300">300</li> <li class="bg" id="select8" value="400">400</li> </ul> </div> <div class="title"> <h2 id="h5-1">選擇背景顏色:</h2> <h2 id="h5-2">選擇寬度(PX):</h2> <h2 id="h5-1">選擇高度(PX):</h2> </div> <div id="sub"> <div id="reset" title="重置">重置</div> <div id="submit" title="確定">確定</div> </div> </div> </body> </html>
相關文章
- dom操作程式碼例項
- JavaScript in運算子程式碼例項JavaScript
- JavaScript運動框架程式碼例項JavaScript框架
- JavaScript取餘數程式碼例項JavaScript
- JavaScript 阻止事件冒泡程式碼例項JavaScript事件
- JavaScript陣列合並程式碼例項JavaScript陣列
- JavaScript倒數計時程式碼例項JavaScript
- JavaScript 表單驗證程式碼例項JavaScript
- JavaScript刪除元素節點程式碼例項JavaScript
- JavaScript中常用的事件程式碼及例項JavaScript事件
- JavaScript DOM、BOM操作JavaScript
- 前端學習程式碼例項-JavaScript阻止事件冒泡前端JavaScript事件
- JavaScript隨滑鼠晃動的div塊程式碼例項JavaScript
- JavaScript讀取文字檔案內容程式碼例項JavaScript
- 前端學習程式碼例項-JavaScript 生成隨機數前端JavaScript隨機
- JavaScript基礎之DOM操作JavaScript
- JavaScript5:常用DOM操作JavaScript
- JavaScript操作DOM常用的APIJavaScriptAPI
- 【JavaScript】DOM之樣式操作JavaScript
- JavaScript 操作DOM效能優化JavaScript優化
- JavaScript入門⑦-DOM操作大全JavaScript
- JavaScript 點選複製選中文字程式碼例項JavaScript
- 小程式操作dom
- css梯形程式碼例項CSS
- 1.14 JavaScript5:常用DOM操作JavaScript
- JavaScript ----- 操作DOM物件的屬性JavaScript物件
- 設計模式例項程式碼設計模式
- table表格美化程式碼例項
- 好程式設計師web前端分享在HTML中使用JavaScript例項程式碼程式設計師Web前端HTMLJavaScript
- [譯]13 種有用的 JavaScript DOM 操作JavaScript
- JavaScript 例項屬性JavaScript
- 純css tab選項卡程式碼例項CSS
- jQuery tab選項卡效果程式碼例項jQuery
- 使用DOM Breakpoints找到修改屬性的Javascript程式碼JavaScript
- 前端學習程式碼例項-JavaScript 阻止擊超連結的跳轉前端JavaScript
- canvas刮刮樂程式碼例項Canvas
- canvas載入效果程式碼例項Canvas
- CSS空心箭頭程式碼例項CSS
- jQuery.map()方法程式碼例項jQuery