简体   繁体   English

IE7 CSS阻止带有Javascript错误的相对位置

[英]IE7 css block relative position with Javascript bug

I have a simple JS script that moves a CSS block at a specific path when a value is given to it. 我有一个简单的JS脚本,当给它一个值时,它将CSS块移动到特定的路径。

You can take a look at the code here http://jsfiddle.net/rayshinn/6DGfb/ 您可以在这里查看代码http://jsfiddle.net/rayshinn/6DGfb/

This code seem to work fine on Chrome and Firefox except IE7. 除了IE7,此代码似乎在Chrome和Firefox上都能正常工作。

The error I get from IE is as follows 我从IE收到的错误如下

Line: 27  
Char:13  
Error: Object doesn't support this property or method  
Code: 0  
URL: http://localhost/test/js/plot.js

Line 27 is as follows 第27行如下

    marker = document.getElementById("marker");
    this.setPosition(INITIAL_X, INITIAL_Y);

Below is my full JS script for your reference. 以下是我完整的JS脚本,供您参考。

(function () {
    var INITIAL_X = 550,
        INITIAL_Y = 152;

    // f(v) -> {"x" : x, "y" : y}
    var calculatePosition = function (value) {
        var result = {};

        result.x = INITIAL_X -  value / 9;
        result.y = INITIAL_Y + 0.117  * value/ 9 ;


        return result;
    }

    var map = {
        marker : null,
        value : 0,

        setPosition : function (x, y) {
             marker.style.left = x + "px";
             marker.style.top  = y + "px";
        },

        init : function () {
            marker = document.getElementById("marker");
            this.setPosition(INITIAL_X, INITIAL_Y);
        },

        increment : function () {
            this.value++;
            var result = calculatePosition(this.value);
            this.setPosition(result.x, result.y);
        },

        decrement : function() {
            this.value--;
            var result = calculatePosition(this.value);
            this.setPosition(result.x, result.y);
        }
    };

    map.init();

    for (var i  = 0; i < 100; i++) {
        map.increment();
    }
})();

Thank you for taking your time to read this and helping me solve this issue. 感谢您抽出宝贵的时间阅读本文档,并帮助我解决了这个问题。 As always any suggestions will be greatly appreciated! 与往常一样,任何建议将不胜感激!

The problem is the line 问题是线

marker = document.getElementById("marker");

marker does not resolve to a property of your map object as your code seems to expect; marker无法解析为map对象的属性,正如您的代码似乎期望的那样; instead, it resolves to a property of the global object. 而是解析为全局对象的属性。 IE, however, populates the global object with properties whose names corresponding to the IDs of elements within the page and then doesn't allow you to overwrite these. IE,但是,使用其名称与页面中元素ID对应的属性填充全局对象,然后不允许您覆盖这些属性。 This means that in IE there is already a global marker that cannot be overwritten. 这意味着在IE中已经存在无法覆盖的全局marker

This is one good reason why implied globals like your marker should be avoided. 这是为什么应该避免像您的marker这样的隐含全局变量的好理由。 The easiest fix is to change references to marker to this.marker : 最简单的解决方法是this.marker marker引用更改为this.marker

    setPosition : function (x, y) {
         this.marker.style.left = x + "px";
         this.marker.style.top  = y + "px";
    },

    init : function () {
        this.marker = document.getElementById("marker");
        this.setPosition(INITIAL_X, INITIAL_Y);
    },

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

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