繁体   English   中英

如何在IE8中获取元素的样式坐标

[英]How to get style coordinates of element in IE8

我已经为此努力了一段时间,所以我需要聪明人的帮助。

我正在努力提取元素中“背景位置”值的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像素值。 显然,我已经通过可以在这里找到的所有Microsoft 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中进行测试,因为我的客户仍在使用IE8。此外,服务器端代码(这是我没有完全权限的共享点服务器)似乎正在强制页面以IE8模式显示。

我还尝试添加html5标签<!doctype html>以强制浏览器以html5呈现。 仍然停留在IE8模式下。 无论如何,谁能指出我对DOM API的调用,该调用将起作用并提取IE8中的像素值?

我制作了一个测试页,其中以内联样式定义了背景位置,并且能够在.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>

IE11开发人员工具的屏幕快照,显示document.getElementById(“ a”)。style.backgroundPosition的值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM