如何获得某个元素的css属性。
来源:互联网 发布:mac无法制作win7系统 编辑:程序博客网 时间:2024/04/27 09:54
先贴个链接:
http://www.quirksmode.org/dom/getstyles.html
原文第一句话说的就是我的疑惑。。。
“Sometimes you'll want to see what styles the default document view has. For instance, you gave a paragraph an width
of 50%, but how do you see how many pixels that is in your users' browser?”
看完上面一大部分讲offsetHeight等等之类的,收获颇多;
不过觉得下面的用javascript来获得属性的例子更有学习意义:
代码如下:
First we pass the function the id of the HTML element and the style property we wish to access
function getStyle(el,styleProp){
Then we store the HTML element in x
: //从这里可以看出选择的方式是通过传递id参数,用x保存html元素
var x = document.getElementById(el);
First the Explorer way: the currentStyle
of the HTML element: //首先的方法适合ie浏览器使用
if (x.currentStyle)var y = x.currentStyle[styleProp];
Then the Mozilla way: the getComputedStyle()
method, which also works in Opera: //接下来是Firefox和Opera的方法
else if (window.getComputedStyle)var y = document.defaultView.getComputedStyle(x,null).getPropertyValue(styleProp);
Finally return y
to whichever function asked for it (in this page it's the function prepare()
that is called when you submit the form or click the link 'get the style'). //将y的值返回,y中存的就是你想要的style的属性
return y;}
Although this function doesn't yet work well, it's the best you can do.
- 如何获得某个元素的css属性。
- 移除元素的某个css属性
- jquery如何获得选择器结果集中某个元素的索引
- 封装获取某个元素指定css属性值的方法
- magento -- 如何获得某个属性组的所有商品
- dom4j读取某个元素的某个属性
- dom4j 读取某个元素的某个属性
- dom4j读取某个元素的某个属性
- 获取到某个元素的某个style中的属性。
- 获得指定元素的css样式方法
- 获得子对象的元素属性
- webbrowther 获得网页元素的属性
- 如何获得网站的CSS
- 如何通过js获得overflow属性为auto的元素的实际宽度
- w3c系列之CSS(三):CSS属性是如何被应用到元素上的
- 获得tableview某个cell的属性的值的方法
- 获得某个class所有属性
- 对不支持CSS浏览器,如何获得提示信息,使用灵活的ALT属性
- UITableViewController中显示navigationItem按钮
- 线程高级---生产者消费者问题
- 如何成为Android高手(网络点击率在三个月前已经过万,并被到处转载)
- android2.3与android2.2的差别整理
- Ubuntu开启ssh服务
- 如何获得某个元素的css属性。
- 随笔
- 在http协议中传输二进制的数据--base64实现
- 转自StackOverflow:MPI, Sungrid vs JPPF?
- 半期总结
- 更新LisView里面的进度条----Progress进度条
- Overlapped I/O模型深入分析(转)--相当完善的一篇
- 大型视频监控平台的出路的几点简单想法1
- C#/CSharp - 禁用Form的关闭按钮