2

私のHTML:<div id="bar" ></div>

私のCSS:

#bar
{
    border-left-width:150px;
}

私のJS:

function getStyle(el,styleProp)
{
    if(el.currentStyle)var y=el.currentStyle[styleProp];
    else if(window.getComputedStyle)var y=document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
    return y;
}

alert(getStyle(document.getElementById("bar"),"border-left-width"));//Outputs 0px

フィドル: http: //jsfiddle.net/4ABhZ/1

どうすればborder-left-width物件を取得できますか?(私の例では、(Firefoxで)機能していません)

4

3 に答える 3

2

border-left-styleあなたの財産を確認してください。none(デフォルト)に設定されています。次のように設定すると、準備が整いsolidます:http: //jsfiddle.net/Paulpro/4ABhZ/4/

于 2012-06-13T16:11:22.880 に答える
1

古いブラウザをサポートするには、hyphenated-cssをcamelCaseに変更する必要があります。

他のブラウザでもキャメルケースを使用して、getComputedStyleオブジェクトのプロパティを直接読み取ることができます。

function getStyle(el, css){
    if(window.getComputedStyle) return getComputedStyle(el, '')[css];
    if(el.currentStyle) return el.currentStyle[css];    
}

alert(getStyle(document.getElementById('bar')、'borderTopWidth'));

注-css定義には、境界線の幅を計算するためのスタイルと幅が必要です(また、寸法を計算するときにdisplay:noneに設定することはできません...)

于 2012-06-13T18:09:07.043 に答える
-1

jQueryを使用する

http://api.jquery.com/category/css/
http://api.jquery.com/css/

これは機能します:http://jsfiddle.net/L2ZwD/1

于 2012-06-13T16:05:39.423 に答える