關(guān)于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實(shí)例
通過(guò)style屬性設(shè)置背景圖案
<!--html--> <div id="change"> change color </div>
/*css*/ #change { border: 1px solid black; width: 200px; height: 200px; text-align: center; line-height: 200px; }
//js change.style.backgroundColor="purple";
在側(cè)邊欄設(shè)置一個(gè)顏色選擇器,將change的背景顏色設(shè)置為選擇的顏色,此時(shí)顏色選擇器的顏色是使用內(nèi)聯(lián)樣式的方式添加的。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>css</title> <style> * { margin: 0; padding: 0; } .wrap { width: 220px; height: 200px; position: absolute; top: 300px; left: -172px; } .open-close { height: 45px; width: 48px; background: url("open-close.png") no-repeat; background-size: contain; border: 1px solid grey; border-left: none; position: absolute; top: 0; right: 0; z-index: 2; } .changer { height: 150px; width: 170px; position: absolute; top: 0; left: 0; border: 1px solid grey; text-align: center; padding-top: 8px; } .list > li { display: block; width: 36px; height: 36px; float: left; margin-left: 9%; margin-top: 10%; } #change { border: 1px solid black; width: 200px; height: 200px; text-align: center; line-height: 200px; } </style> </head> <body> <div class="wrap" id="wrap"> <!--html--> <div class="open-close" id="open"></div> <div class="changer"> <span>顏色選擇器</span> <ul class="list"> <li class="color-orange" style="background-color: orange"></li> <li class="color-red" style="background-color: red"></li> <li class="color-blue" style="background-color: blue"></li> <li class="color-black" style="background-color: black"></li> <li class="color-green" style="background-color: green"></li> <li class="color-pink" style="background-color: pink"></li> </ul> </div> </div> <div id="change"> change color </div> <script> var open = document.getElementById("open"); var wrap = document.getElementById("wrap"); var list = document.getElementById("list"); var change = document.getElementById("change"); var color_change = document.getElementsByTagName("li"); change.style.backgroundColor = "purple"; open.onmouseover = function () { wrap.style.left = 0 + "px"; }; open.onclick = function () { wrap.style.left = -172 + "px"; }; for (var i = 0; i < color_change.length; i++) { color_change[i].id = i; color_change[i].onclick = function () { change.style.backgroundColor = color_change[this.id].style.backgroundColor; } } </script> </body> </html>
問(wèn)題:
當(dāng)顏色選擇器的顏色是使用嵌入式或者外部引入的方式添加時(shí),javascript的style屬性無(wú)效,獲取不到顏色值。
解決方法:
javascript的style屬性只能獲取內(nèi)聯(lián)樣式,對(duì)于外部引入樣式和嵌入式樣式需要用currentStyle屬性。但是,currentStyle在Firefox和Chrome下不支持,需要使用如下兼容性代碼:
HTMLElement.prototype.__defineGetter__("currentStyle", function () { return this.ownerDocument.defaultView.getComputedStyle(this, null); });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> * { margin: 0; padding: 0; } .wrap { width: 220px; height: 200px; position: absolute; top: 300px; left: -172px; } .open-close { height: 45px; width: 48px; background: url("open-close.png") no-repeat; background-size: contain; border: 1px solid grey; border-left: none; position: absolute; top: 0; right: 0; z-index: 2; } .changer { height: 150px; width: 170px; position: absolute; top: 0; left: 0; border: 1px solid grey; text-align: center; padding-top: 8px; } .list > li { display: block; width: 36px; height: 36px; float: left; margin-left: 9%; margin-top: 10%; } .color-orange { background-color: orange; } .color-red { background-color: red; } .color-blue { background-color: blue; } .color-blank { background-color: black; } .color-green { background-color: green; } .color-pink { background-color: pink; } #change { border: 1px solid black; width: 200px; height: 200px; text-align: center; line-height: 200px; } </style> </head> <body> <div class="wrap" id="wrap"> <!--html--> <div class="open-close" id="open"></div> <div class="changer"> <span>顏色的選擇</span> <ul class="list"> <li class="color-orange"></li> <li class="color-red"></li> <li class="color-blue"></li> <li class="color-blank"></li> <li class="color-green"></li> <li class="color-pink"></li> </ul> </div> </div> <div id="change"> change color </div> <script> HTMLElement.prototype.__defineGetter__("currentStyle", function () { return this.ownerDocument.defaultView.getComputedStyle(this, null); }); var open = document.getElementById("open"); var wrap = document.getElementById("wrap"); var list = document.getElementById("list"); var change = document.getElementById("change"); var color_change = document.getElementsByTagName("li"); change.style.backgroundColor="purple"; open.onmouseover = function () { wrap.style.left = 0 + "px"; }; open.onclick = function () { wrap.style.left = -172 + "px"; }; for (var i = 0; i < color_change.length; i++) { color_change[i].id = i; color_change[i].onclick = function () { change.style.backgroundColor = color_change[this.id].currentStyle.backgroundColor; } } </script> </body> </html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 嵌入式C實(shí)戰(zhàn)項(xiàng)目開發(fā)技巧:對(duì)一個(gè)有規(guī)律的數(shù)組表進(jìn)行位移操作的方法
- 嵌入式項(xiàng)目使用C語(yǔ)言結(jié)構(gòu)體位段特性實(shí)現(xiàn)斷言宏校驗(yàn)數(shù)據(jù)范圍有效性的方法
- JS實(shí)現(xiàn)PC手機(jī)端和嵌入式滑動(dòng)拼圖驗(yàn)證碼三種效果
- JavaScript基本的輸出和嵌入式寫法教程
- javascript動(dòng)態(tài)添加樣式(行內(nèi)式/嵌入式/外鏈?zhǔn)降纫?guī)則)
- 嵌入式C語(yǔ)言查表法在項(xiàng)目中的應(yīng)用
相關(guān)文章
javascript instanceof 內(nèi)部機(jī)制探析
在 JavaScript 中,可以用 instanceof 來(lái)判斷一個(gè)對(duì)象是不是某個(gè)類或其子類的實(shí)例。2010-10-10兩個(gè)select多選模式的選項(xiàng)相互移動(dòng)(示例代碼)
本篇文章主要是對(duì)兩個(gè)select多選模式的選項(xiàng)相互移動(dòng)示例代碼進(jìn)行了介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01基于JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的事件觸發(fā)器
這篇文章主要為大家詳細(xì)介紹了如何使用JavaScript實(shí)現(xiàn)一個(gè)簡(jiǎn)單的事件觸發(fā)器,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01JS實(shí)現(xiàn)的簡(jiǎn)單圖片切換功能示例【測(cè)試可用】
這篇文章主要介紹了JS實(shí)現(xiàn)的簡(jiǎn)單圖片切換功能,結(jié)合實(shí)例形式分析了javascript結(jié)合時(shí)間函數(shù)定時(shí)觸發(fā)控制圖片的遍歷與切換操作相關(guān)技巧,需要的朋友可以參考下2017-02-02JavaScript設(shè)計(jì)模式之抽象工廠模式介紹
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之抽象工廠模式介紹,抽象工廠模式就是對(duì)功能類單獨(dú)創(chuàng)建工廠類,這樣就不必修改之前的代碼,又?jǐn)U展了功能,需要的朋友可以參考下2014-12-12微信小程序跳轉(zhuǎn)到其他網(wǎng)頁(yè)(外部鏈接)的實(shí)現(xiàn)方法
這篇文章主要介紹了微信小程序跳轉(zhuǎn)到其他網(wǎng)頁(yè)(外部鏈接)的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09一個(gè)JS函數(shù)搞定網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果
這篇文章主要介紹了使用JS函數(shù)實(shí)現(xiàn)網(wǎng)頁(yè)標(biāo)題(title)閃動(dòng)效果的代碼,需要的朋友可以參考下2014-05-05