document.getElementById("div1").attachEvent("onmouseover",change());以上语句动态绑定了一个事件和触发函数但是问题是:  按道理这语句   仅仅是绑定,  不会执行change()里的内容的,但是我这里    却却在绑定的同时又执行了change()函数的内容一遍,为什么呢?  我的鼠标根本没有进入div1的区域,按道理不会触发change()函数

解决方案 »

  1.   

    绑定的时候不用使用(),使用(),这个代表调用这个函数
    如:document.getElementById("div1").attachEvent("onmouseover",change);
      

  2.   

    想带参数可以这样写:
    document.getElementById("div1").attachEvent("onmouseover",function(){change("参数");});
      

  3.   

    这样写可以不改变作用域
    document.getElementById("div1").attachEvent("onmouseover",function(){change.call(this,"参数");});
      

  4.   

    document.getElementById("div1").attachEvent("onchange",function(){})
      

  5.   

    document.getElementById("div1").attachEvent("onmouseover",function(){change());
    这样就可以了
      

  6.   

    事件绑定通用写法addEventListener = function(control, eventName, fn) {
                    // IE下
    if (window.attachEvent) {
    control.attachEvent('on' + eventName, function(e) {fn.call(control, e);});

                    // Firefox and Chrome
    } else if (window.addEventListener) {
            control.addEventListener(eventName, fn, false);

                    // 一般情况
    } else {
    control['on' + eventName] = fn;
    }
    };调用的时候
    var div1 = document.getElementById("div1")
    addEventListener(div1, 'click', function() {
       change(参数);
    });IE中的attachEvent有个问题。document.getElementById("div1").attachEvent("onmouseover",change);
    中change如果有用到this,会把this直接连到window对象而不是事件绑定的对象(比如上例中的div1)。所以,用fn.call()来解决这个问题。
      

  7.   

    document.createElement("div").onclick=function(){H_Tab_Web_Click(i)};
    document.createElement("div").onmouseover=function(){H_Tab_Web_over(i)};
    document.createElement("div").onmouseout=function(){H_Tab_Web_out(i)};
    需要注意的一点是:这里的i的作用域范围,最好i是在某个函数里的某个函数里定义的变量!!!!!!
      

  8.   

    var index = 0;
    function test() {
       // 0
       alert(index);
    }
    test();function test() {
       var index = 0;
    }
    test();
    // undefined
    alert(index);不知道你说的某个函数的某个函数时什么意思。
    这个就跟java内部类的原理是一样的。里面的可以调用外面的,外面的看不到里面的var index = 0;
    var div = document.createElement("div");
    div.onclick = function(i) {
        return function() {
            H_Tab_Web_Click(i);
        };
    }(index);
    如果是这样的想法,在javascript里面也成立,但是真的有必要那么复杂吗?
      

  9.   

      function addFile() {
                
                var div = document.createElement("div");
                var f = document.createElement("input");
                f.setAttribute("type", "file");
                f.setAttribute("name", "File");
                f.setAttribute("size", "50");
                div.appendChild(f);                     var d = document.createElement("input");
                d.setAttribute("type", "button");
                d.attachEvent("onclick", deteFile, this);
                d.setAttribute("value", "移除");
                div.appendChild(d);         
                document.getElementById("divupload").appendChild(div);    }
        function deteFile(o) {       
            o = o.srcElement;
            if (o.tagName != "DIV") o = o.parentNode;
            o.parentNode.removeChild(o);
        }