Why when I add an event as a function like this:
function func(arg)
{
arg.style.marginLeft = "65px";
}
window.onload = function() {
var test = document.getElementById("aaa");
test.onmouseover = func(test);
}
It's been executed immediately (even if I do not hover the element).
But this one works:
window.onload = function() {
var test = document.getElementById("aaa");
test.onmouseover = function() {
test.style.marginLeft = "65px";
}
}
onloadandonmouseoveris kind of brittle. If you or any other code ever want to add a second handler, the first one is lost. Consider usingaddEventListener(orattachEventin IE), or using a library like jQuery which handwaves the problems away.