`
luolonghao
  • 浏览: 118228 次
  • 性别: Icon_minigender_1
  • 来自: Shanghai
社区版块
存档分类
最新评论

safari和chrome取不到style.border,是BUG?

阅读更多
<div id="test_div" style="border:1px solid #000000;"></div>
<script type="text/javascript">
alert(document.getElementById("test_div").style.border);
</script>

safari和chrome上运行alert出来结果为空,IE、FF、Opera都有值。

演示: http://kindsoft.net/test/style_border_test.html

奇怪的是style.borderTop可以取得border,但style.borderLeft,style.borderBottom,style.borderRight都不行。

浏览器版本:
Windows XP, safari 3.2
Windows XP, chrome 1.0.154.36
分享到:
评论
3 楼 汪兆铭 2009-01-20  
刚在写那代码呢。不好意思。。我写得比较慢。。

<html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=gb2312">
      <title>style border test</title>
      <script>
      function getStyleAtt(id,styleAtt){
        var style = document.getElementById(id).getAttribute("style");
        var regStr = "/"+styleAtt+":(\s*w+\s*);/g";
        var pattern = new RegExp(styleAtt+":\s*([^;]+)");
        var matches = style.match(pattern);
        var ret = null;
        if(matches && matches.length > 0) ret = matches[1];
        return ret
      }
      </script>
    </head>
    <body>
        <div id="test_div" style="border: 1px solid rgb(0, 0, 0); font:icon; background:#333333">hello</div>
        <script>
           alert("test_div.border: "+getStyleAtt("test_div","border")
		   		+"\ntest_div.background: "+getStyleAtt("test_div","background"));
        </script>
    </body>
</html>
2 楼 luolonghao 2009-01-07  
这个是Webkit的BUG,已经修复好了,只不过Safari和Chrome还没用最新版本Webkit,所以问题还在。

https://bugs.webkit.org/show_bug.cgi?id=15823
1 楼 achun 2009-01-06  
<p>截取一段代码给你参考</p>
<pre name='code' class='js'>/*获取elem的某个style属性*/

function getStyle(elem, property){

if (property=='class') property='className';

else property=this.camelize(property);

if (elem.currentStyle)// IE5+

return elem.currentStyle[property];

if(elem.style[property])

return elem.style[property];

if (document.defaultView.getComputedStyle)// FF/Mozilla

var currentStyle = document.defaultView.getComputedStyle(elem, null);

else if (window.getComputedStyle)// NS6+

var currentStyle = window.getComputedStyle(elem, null);

return currentStyle[property] || currentStyle.getPropertyValue(this.uncamelize(property));

}</pre>
 

相关推荐

Global site tag (gtag.js) - Google Analytics