document.getElementById("div1").attachEvent("onmouseover",change());以上语句动态绑定了一个事件和触发函数但是问题是: 按道理这语句 仅仅是绑定, 不会执行change()里的内容的,但是我这里 却却在绑定的同时又执行了change()函数的内容一遍,为什么呢? 我的鼠标根本没有进入div1的区域,按道理不会触发change()函数
解决方案 »
- 怎样比较多个标签获取并获得最低高度的对象
- 页面加载特效用jquery怎么做
- 关于javaweb开发的咨询,请大家解惑,感谢
- js里面说的"直接量"是什么意思,请详细讲解!
- javascript 通过读取文件,并更新网页 怎么做呢
- 当多用户同时操作同一个记录
- 求一个效率较高的忽略大小写的字符串比较函数
- 怎样把jsp页面上的内容导入到word或excel中??但不降低客户端安全级别!
- 请问可以用insertAdjacentHTML在一个页面里插入一个JS特效吗?
- 为什么IE中无法显示jquery动画效果,其他浏览器可以啊??
- JS高手帮忙,大家也可讨论下实现。
- 在textarea中每行数据后面加上X,表示删除本行。求这种效果!
如:document.getElementById("div1").attachEvent("onmouseover",change);
document.getElementById("div1").attachEvent("onmouseover",function(){change("参数");});
document.getElementById("div1").attachEvent("onmouseover",function(){change.call(this,"参数");});
这样就可以了
// 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()来解决这个问题。
document.createElement("div").onmouseover=function(){H_Tab_Web_over(i)};
document.createElement("div").onmouseout=function(){H_Tab_Web_out(i)};
需要注意的一点是:这里的i的作用域范围,最好i是在某个函数里的某个函数里定义的变量!!!!!!
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里面也成立,但是真的有必要那么复杂吗?
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);
}