2016-04-19 24 views
0

私はこれで頭を強くしていましたので、スマートな人たちの助けが必要です。IE8で要素のスタイル座標を取得する方法

私は要素の 'background-position'値のxy座標を抽出するのに苦労しています。これをテストするために、私は(css_class =所定のcssクラス)​​これを設定:

var d = document.getElementById(css_class); 

alert("1: "+getStyle(d,'background-position')); 
alert("2: "+d.style.backgroundPosition); 
alert("3: "+d.currentStyle.backgroundPosition); 
alert("4: "+d.style.cssText); 
alert("5: "+window.getComputedStyle(d,null).getPropertyValue('background-position')); 

test1--> undefined 
test2--> blank 
test3--> undefined 
test4--> blank 
test5-->'Object doesn't support property or method 'getComputedStyle'' 

何が私のxyピクセル値をバック与えません。明らかに私は私が見つけることができるすべてのマイクロソフトのDOM APIリファレンスを介して私の研究を行ってきました(下のgetStyle関数を取得していますが、getComputedStyleも使用しています)、いくつかのmozilla開発者フォーラムなどを検索しました。 これは関数私はこのフォーラムで見つかった:

function getStyle(el,styleProp) { 
     var camelize = function (str) { 
      return str.replace(/\-(\w)/g, function(str, letter){ 
       return letter.toUpperCase(); 
       }); 
      }; 

      if (el.currentStyle) { 
      return el.currentStyle[camelize(styleProp)]; 
      } else if (document.defaultView && document.defaultView.getComputedStyle) { 
      return document.defaultView.getComputedStyle(el,null) 
             .getPropertyValue(styleProp); 
      } else { 
      return el.style[camelize(styleProp)]; 
      } 
     } 

基本的に私は私のIE 11ブラウザはIE8モード(デベロッパーコンソールになると言う)に戻りますので、この問題はかなり確信しているChromeとMozillaの中にこれらの作業のほとんどはうまく。私はIE8でテストしています。私の顧客が引き続きInternet Explorer 8を使用しているからです。また、サーバー側のコード(これは完全な権限を持たないSharepointサーバーです)がページをIE8モードで表示するようになっています。

また、html5タグ<!doctype html>を追加して、おそらくブラウザをhtml5でレンダリングさせようとしました。まだIE8モードに固執しています。とにかく、誰も私はIE8のピクセル値を引き出すと動作するDOM API呼び出しに私を指すことができますか?

答えて

0

background-positionがインラインスタイルで定義されているテストページを作成しました。これを.style.backgroundPositionプロパティで取得できました。 (私はそれがローカルのファイルなので、JavaScriptを有効にする]ボタンをクリックする必要がありました。)

<html> 
<head> 
    <meta http-equiv="x-ua-compatible" content="ie=8"> 
    <title>ie8 test</title> 
</head> 
<body> 
    <div id="a" style="width: 200px; height: 200px; border: 1px outset gray; background-image: url('./Untitled.png'); background-position: 10px 10px;"></div> 
    <script> 
     document.write(document.getElementById("a").style.backgroundPosition); 
    </script> 
</body> 
</html> 

Screenshot of IE11's developer tools, showing the value of document.getElementById("a").style.backgroundPosition

関連する問題