概述
一般给元素设置行内样式,如<div id="div1" style="width:500px;"></div>。如要获取它的样式,即可document.getElementById("div1").style.width来获取或设置。但是如果样式是在外链link中的或者是页面的非行内样式,就获取不到了。
在标准浏览器中可以通过window.getComputedStyll(obj,null)[property]来获取外链样式,但是在ie浏览器中则是通过obj.currentStyle来获取。
完整html代码:
<!DOCTYPE html> <html> <head> <title>js获取元素的外链样式</title><base target="_blank"/> <style type="text/css"> p { width: 500px; line-height: 30px; } </style> <script src="http://uoften.com/jquery/jquery-1.11.2.min.js"> </script> <script> function getstyle(obj,property){ if(obj.currentStyle){ return obj.currentStyle[property]; }else if(window.getComputedStyle){ return document.defaultView.getComputedStyle(obj,null)[property];//或者也可以通过window.getComputedStyle来获取样式 } return null; } $(document).ready(function(){ $("p").click(function(){ alert(getstyle(this,"width")); }); }); </script> </head> <body> <p style="width:750px;">如果您点击我,弹出宽度。</p> <p>点击我,弹出宽度。</p> <p>也要点击我哦。</p> <div><a href="http://uoften.com">首页</a> <a href="http://uoften.com/phtml/">特效库</a> <a href="http://uoften.com/a/bjae/nb9lb3sd.htm">原文</a></div> </body> </html>
以上这篇js获取元素的外链样式的简单实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
最后
以上就是纯真眼神为你收集整理的js获取元素的外链样式的简单实现方法的全部内容,希望文章能够帮你解决js获取元素的外链样式的简单实现方法所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复