document.getElementById("div1").attachEvent("onmouseover",change());以上语句动态绑定了一个事件和触发函数但是问题是: 按道理这语句 仅仅是绑定, 不会执行change()里的内容的,但是我这里 却却在绑定的同时又执行了change()函数的内容一遍,为什么呢? 我的鼠标根本没有进入div1的区域,按道理不会触发change()函数
解决方案 »
- 动态添加toggle疑问
- 怎么用js设置img标记的src?
- 请问怎么将数据存到HTML 文件中更方便?
- 如何能得到xmlhttp.responseText里面的某个元素的内容?
- 你能列出多少种 页面跳转,还有关闭窗口问题?
- 如何启动javascript脚本禁用???
- 如何用js实现自动统计字符个数
- 我想在进入一个窗体后再把本窗体的工具栏等去掉,该怎么做?
- 把整数转成字符串或是把字符串转成整数的函数是什么?
- 求例子:页面左面有一个树形菜单,可以隐藏,隐藏后右面内容占据整个页面,弹出后,右面内容不能被挡住!
- 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);
}