關(guān)于javascript獲取內(nèi)聯(lián)樣式與嵌入式樣式的實例
通過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è)置一個顏色選擇器,將change的背景顏色設(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>
問題:
當(dāng)顏色選擇器的顏色是使用嵌入式或者外部引入的方式添加時,javascript的style屬性無效,獲取不到顏色值。
解決方法:
javascript的style屬性只能獲取內(nèi)聯(lián)樣式,對于外部引入樣式和嵌入式樣式需要用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>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript動態(tài)修改網(wǎng)頁元素內(nèi)容的方法
這篇文章主要介紹了JavaScript動態(tài)修改網(wǎng)頁元素內(nèi)容的方法,實例分析了javascript操作html元素的技巧,具有一定參考借鑒價值,需要的朋友可以參考下2015-03-03
echarts數(shù)據(jù)可視化實現(xiàn)多個柱狀堆疊圖頂部顯示總數(shù)示例
這篇文章主要為大家介紹了echarts實現(xiàn)多個柱狀堆疊圖頂部顯示總數(shù)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
JS的replace方法與正則表達(dá)式結(jié)合應(yīng)用講解
replace方法的語法是:stringObj.replace(rgExp, replaceText) 其中stringObj是字符串(string),reExp可以是正則表達(dá)式對象(RegExp)也可以是字符串(string),replaceText是替代查找到的字符串。。為了幫助大家更好的理解,下面舉個簡單例子說明一下2007-12-12

