首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Internet Explorer 8.事件

Internet Explorer 8.事件
EN

Stack Overflow用户
提问于 2012-09-11 20:35:30
回答 1查看 743关注 0票数 0

所以我有这个网站,它是使用sencha编辑器(Sencha Animator)创建的。

就此而言,他们并不真正支持IE,所以我决定对其进行自定义编辑,因为我们的大多数客户端仍然使用IE7+。

现在的主要问题是它在Chrome、Safari、FF和IE8/9中加载正常,但似乎所有事件都不起作用。

我首先尝试做一些条件检查(我在这里找到的),看看它是什么浏览器,并添加像这样的事件:

代码语言:javascript
复制
function bindEvent(el, eventName, eventHandler) {
   if (el.addEventListener){
       el.addEventListener(eventName, eventHandler, false); 
   } else if (el.attachEvent){
       el.attachEvent("on"+eventName, eventHandler);
   }
}

不知何故,这段代码突然中断了

代码语言:javascript
复制
if (el.addEventListener) {

所以在那之后,我想等等,jQuery应该有这个内置的。所以我添加了来自谷歌的jQuery的CDN。

在我的JS中我添加了

代码语言:javascript
复制
$(document).ready(function () {
   // load my start function here
});

这是可行的,所以至少页面加载了所有的内容。

现在我想使用

代码语言:javascript
复制
$(element).click(function () {
   alert('hello');
});

但这在IE和Firefox中根本不起作用( mouseup也不起作用) (IE在这种情况下比FF工作得更好)

我再也没有头绪了,整个IE的事情破坏了一切,一个只在Chrome上工作的网站是不可能的……这真的很令人沮丧:

可在此处找到该网站的链接:Link

以下是完整的JS代码:

代码语言:javascript
复制
if (typeof (AN) === 'undefined') {
   AN = {};
}
AN.Controller = {
   scenes: {},
   scenesArray: [],
   currentSceneID: -1,
   olElement: null,
   events: {},
   useOrmma: false,
   setConfig: function (configData) {
      this.events = configData.events;
      this.olElement = document.getElementById(configData.parentId);
      var liElements = this.olElement.children;
      if (configData.ormma) {
        this.useOrmma = true;
    }
    var scene;
    for (var i = 0; i < configData.scenes.length; i++) {
        scene = configData.scenes[i];
        scene.element = liElements[i];
        this.scenes[scene.id] = scene;
        this.scenesArray.push(scene);
    }
    this.setupListeners();
    this.startSceneByID(this.scenesArray[0].id);
},
runningAnimationCount: 0,
browser: 'webkit',
setupListeners: function () {
    var me = this;
    var eventName = "webkitAnimationEnd";
    if (document.body.style.MozAnimationName !== undefined) {
        eventName = "animationend";
        this.browser = "moz";
    }

    function addMousemoveListenerTo(scene) {
        /*bindEvent(scene.element, 'mousemove', function (event) {
            scene.mousemoveAction(me, event);
        }, false);*/

    }
    var scene;
    for (var i = 0; i < this.scenesArray.length; i++) {
        scene = this.scenesArray[i];
        if (scene.mousemoveAction) {
            addMousemoveListenerTo(scene);
        }
    }

    function addListenerTo(element, eventType, aFunction) {
        /*bindEvent(element, eventType, function (event) {
            aFunction(me, event);
        });*/
        $("#AN-sObj-17984").live("click", function(){
            AN.Controller.startSceneByID(2);
        });
    }

    var element, event;
    for (var i = 0; i < this.events.length; i++) {
        event = this.events[i];
        element = document.getElementById(event.id);
        addListenerTo(element, event.type, event.handler);
    }
},
onAnimationEnd: function () {
    this.runningAnimationCount--;
    if (this.runningAnimationCount === 0) {
        this.onSceneFinish();
    }
},
startSceneByID: function (sceneID) {
    var me = this;
    if (sceneID === this.currentSceneID) {
        this.scenes[sceneID].element.setAttribute('class', 'run restart');
        setTimeout(function () {
            me.runningAnimationCount = me.scenes[sceneID].animationCount;
            me.scenes[sceneID].element.setAttribute('class', 'run');
            if (me.scenes[sceneID].startAction) {
                me.scenes[sceneID].startAction(me);
            }
            if (me.scenes[sceneID].animationCount === 0) {
                me.onSceneFinish();
            }
        }, 0);
        return;
    } else if (this.currentSceneID !== -1) {
        this.scenes[this.currentSceneID].element.setAttribute('class', '');
    }
    this.runningAnimationCount = this.scenes[sceneID].animationCount;
    this.currentSceneID = sceneID;
    var nextScene = this.scenes[sceneID];
    if (this.browser === 'moz') {
        nextScene.element.setAttribute('class', 'run restart');
        var unused = nextScene.element.offsetHeight;
        nextScene.element.setAttribute('class', 'run');
    } else {
        console.log(nextScene.element);
        nextScene.element.setAttribute('class', 'run');
    }
    if (this.useOrmma) {
        this.ormmaNextScene(nextScene);
    }
    if (nextScene.startAction) {
        nextScene.startAction(this);
    }
    if (nextScene.animationCount === 0) {
        this.onSceneFinish();
    }
},
replayScene: function () {
    this.startSceneByID(this.currentSceneID);
},
onSceneFinish: function () {
    if (this.scenes[this.currentSceneID].endAction) {
        this.scenes[this.currentSceneID].endAction(this);
    }
},
goToNextScene: function () {
    var nextIndex = this.scenesArray.indexOf(this.scenes[this.currentSceneID]) + 1;
    var nextScene;
    if (nextScene = this.scenesArray[nextIndex]) {
        this.startSceneByID(nextScene.id);
    }
},
goToURL: function (aURL) {
    document.location.href = aURL;
},
ormmaNextScene: function (nextScene) {
    var currentState = ormma.getState();
    if (nextScene.dimensions.expanded) {
        var maxSize = ormma.getMaxSize();
        if (currentState !== 'expanded') {
            ormma.expand({
                x: 0,
                y: 0,
                width: maxSize.width,
                height: maxSize.height
            });
        }
        var transform = "";
        var elementHeight = nextScene.element.offsetHeight;
        var elementWidth = nextScene.element.offsetWidth;
        var y = (maxSize.height - elementHeight) / 2;
        var x = (maxSize.width - elementWidth) / 2;
        transform += " translate3d(" + Math.round(x) + "px," + Math.round(y) + "px,0)";
        if (nextScene.dimensions.fit) {
            var scaleFactor = Math.min(maxSize.width / elementWidth, maxSize.height / elementHeight);
            transform += " scale3d(" + scaleFactor + "," + scaleFactor + ",1)";
        }
        nextScene.element.style.webkitTransform = transform;
    } else {
        if (currentState === 'expanded') {
            ormma.close();
        }
        ormma.resize(nextScene.dimensions.width, nextScene.dimensions.height);
    }
}
};

function loadData() {
var configData = {
    parentId: 'AN-sObj-parentOl',
    ormma: false,
    scenes: [//lots of config data
    ]
  };
  AN.Controller.setConfig(configData);
};

$(document).ready(function() {
   loadData();
   $("area[rel^='prettyPhoto']").prettyPhoto({
      social_tools: '',
      show_title: false,
      theme: 'light_rounded'
   });
});`
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-11 21:45:12

代码:

代码语言:javascript
复制
function bindEvent(el, eventName, eventHandler) {
   if (el.addEventListener){
       el.addEventListener(eventName, eventHandler, false); 
   } else if (el.attachEvent){
       el.attachEvent("on"+eventName, eventHandler);
   }
}

完全没问题。在if (el.addEventListener)上失败的唯一原因是el不是DOM元素。

当我转到您引用的实际页面时,我看到报告了一些javascript错误。第一种情况发生是因为当从下面的代码调用时,您将在addListenerTo()中为el传递null

代码语言:javascript
复制
for (var i = 0; i < this.events.length; i++) {
    event = this.events[i];
    element = document.getElementById(event.id);
    addListenerTo(element, event.type, event.handler);
}

这是因为document.getElementById(event.id);在页面中找不到对象。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12369861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档