ITeye博客

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

luolonghao · 2009-01-06 22:13 · KindEditor · Markdown


<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)

  1. #3 楼 · 汪兆铭 · 2009-01-20 11:55

    刚在写那代码呢。不好意思。。我写得比较慢。。

    
    <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. #2 楼 · luolonghao · 2009-01-07 21:57

    这个是Webkit的BUG,已经修复好了,只不过Safari和Chrome还没用最新版本Webkit,所以问题还在。

    https://bugs.webkit.org/show_bug.cgi?id=15823

  3. #1 楼 · achun · 2009-01-06 22:19
    function getStyle(elem, property){
    
    	if (property=='class') property='className';
    
    	else property=this.camelize(property);
    
    	if (elem.currentStyle)// IE5+
    
    		return elem.currentStyle;
    
    	if(elem.style)
    
    		return elem.style;
    
    	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 || currentStyle.getPropertyValue(this.uncamelize(property));
    
    }</pre>
    

评论功能维护中,当前仅展示历史评论。