我一直在這裏嘲笑我一段時間,所以我需要一些聰明人的幫助。如何在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模式(是這麼說的在開發者控制檯)在大多數瀏覽器和Mozilla就好了這些工作。我在IE8中測試,因爲我的客戶仍然使用IE 8.同樣,服務器端代碼(這是我沒有完全權限的sharepoint服務器)強制頁面在IE8模式下顯示。
我也嘗試添加html5標記<!doctype html>
,以便強制瀏覽器在html5中呈現。仍然堅持IE8模式。無論如何,任何人都可以指向我的DOM API調用,將工作,並在IE8中拉出像素值?