本帖最后由 Sun771037278 于 2012-02-28 16:33:30 编辑

解决方案 »

  1. function out() {
      if (window.event.toElement.id != "layer1")
        layer1.style.display = "none";
      }中toElement是IE专用有,需要有兼容写法:在方法中写入event作参数,然后使用var target=event?event.target:window.event.srcElement;取得事件对象,接着对target.id作判断,试试吧.
      

  2. window.onload = function () {
                layer("Span1");
    }
     function layer(id) {
                var layer1 = document.createElement("div"),
                 elem = document.getElementById(id);
                layer1.id = "layer1";
                // ........
                layer1.innerHTML = "layer";
                layer1.style.display = "none";
                elem.parentNode.appendChild(layer1);
            
    addEvent(layer1, 'mouseout', function(event){
    var e = event || window.event,
    relatedElement = event.relatedTarget || event.toElement;
    if(relatedElement.id != "layer1"){
    layer1.style.display = "none";
    }
    });
    addEvent(elem, 'mouseover', function(){
    layer1.style.display = "block";
    });

    }

    function addEvent(el, eventType, fn) {
                if (el.addEventListener) {
                    el.addEventListener(eventType, fn, false);
                } else if (el.attachEvent) {
                    el.attachEvent("on" + eventType, fn);
                } else {
                 if(!el.eventHandle){
                 el.eventHandle = [];
                 }
                 el.eventHandle.push(fn);
                 var arr = el.eventHandle;
                    el["on" + eventType] = function(event){
                     for(var i = 0, len = arr.length; i < len; i++){
                     arr[i](event);
                     }
                    }
                }
            }
      

  3. 直接用jquery,浏览器兼容问题就交给jquery了
      

  4. 要想快就学习学习jquery吧,省事
      

类似问题 »