关于javascript获取内联样式与嵌入式样式的实例
程序员文章站
2024-02-07 10:43:16
通过style属性设置背景图案
change color
通过style属性设置背景图案
<!--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";
在侧边栏设置一个颜色选择器,将change的背景颜色设置为选择的颜色,此时颜色选择器的颜色是使用内联样式的方式添加的。
<!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>
问题:
当颜色选择器的颜色是使用嵌入式或者外部引入的方式添加时,javascript的style属性无效,获取不到颜色值。
解决方法:
javascript的style属性只能获取内联样式,对于外部引入样式和嵌入式样式需要用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>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
关于javascript获取内联样式与嵌入式样式的实例
-
获取内联和链接中的样式(js代码)_javascript技巧
-
关于javascript获取内联样式与嵌入式样式的实例
-
获取内联和链接中的样式(js代码)_javascript技巧
-
javascript如何获取内联样式与嵌入式样式的实例代码分享
-
javascript如何获取内联样式与嵌入式样式的实例代码分享
-
原生javascript获取各种CSS样式属性的方法实例总结
-
原生javascript获取各种CSS样式属性的方法实例总结
-
表单元素获取,dom树的遍历与常用属性,dom元素的增删改, js操作元素内容,留言板实例,dataset对象,获取元素计算样式,classList 对象常用方法 ,事件的添加与派发
-
js获取内联样式的方法_javascript技巧