为什么单独定义的函数不能在“domready”上工作? JavaScript 中的事件?
我对 JavaScript 还很陌生。我正在尝试为某些元素设置相同的高度。除了将函数移到“addEvent”声明之外之外,我设法完成了所有操作。
换句话说,这段代码有效:
window.addEvent('domready', function() {
var elements = $$( 'div#leftcolumn div.module_menu' );
if( elements && elements.length > 1 ) {
var heights = [];
elements.each( function( el ) {
heights.push( el.getStyle('height').toInt() );
});
maxHeight = Math.max.apply( Math, heights ) + "px";
elements.each( function( el ) {
el.setStyle('height', maxHeight );
});
delete(heights);
}
}
);
虽然这段代码不起作用:
function matchHeight( selector ) {
var elements = $$( selector );
if( elements && elements.length > 1 ) {
var heights = [];
elements.each( function( el ) {
heights.push( el.getStyle('height').toInt() );
});
maxHeight = Math.max.apply( Math, heights ) + "px";
elements.each( function( el ) {
el.setStyle('height', maxHeight );
});
delete(heights);
}
}
window.addEvent( 'domready', matchHeight( 'div#leftcolumn div.module_menu' ) );
我已经使用更简单的函数进行了测试并且它有效,例如:
window.addEvent('domready', function() { alert('test'); } )
相当于
function giveMessage() { alert('test'); }
window.addEvent( 'domready', giveMessage())
为什么?
I am quite new to JavaScript. I am trying to set equal heights to some elements. I managed to do everything except moving the function outside the "addEvent" declaration.
In other words, this code works:
window.addEvent('domready', function() {
var elements = $( 'div#leftcolumn div.module_menu' );
if( elements && elements.length > 1 ) {
var heights = [];
elements.each( function( el ) {
heights.push( el.getStyle('height').toInt() );
});
maxHeight = Math.max.apply( Math, heights ) + "px";
elements.each( function( el ) {
el.setStyle('height', maxHeight );
});
delete(heights);
}
}
);
while this code does NOT work:
function matchHeight( selector ) {
var elements = $( selector );
if( elements && elements.length > 1 ) {
var heights = [];
elements.each( function( el ) {
heights.push( el.getStyle('height').toInt() );
});
maxHeight = Math.max.apply( Math, heights ) + "px";
elements.each( function( el ) {
el.setStyle('height', maxHeight );
});
delete(heights);
}
}
window.addEvent( 'domready', matchHeight( 'div#leftcolumn div.module_menu' ) );
I already test with simpler functions and it works, like e.g:
window.addEvent('domready', function() { alert('test'); } )
is equivalent to
function giveMessage() { alert('test'); }
window.addEvent( 'domready', giveMessage())
why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为你需要删除括号;函数是一个对象,您需要传递函数,而不是它的返回值。因此,对于第一个示例,您应该:
相反。对于第二个示例,它是:
同样,您传递对象
giveMessage
,而不是调用它后获得的结果。It's because you need to remove the parenthesis; a function is an object, and you need to pass the function, not its return value. So, for your first example, you should have:
Instead. For your second example, it's:
Again, you pass the object
giveMessage
, not the result obtained after invoking it.您忘记将
matchHeight
调用放入闭包中。将最后一行更改为:You forgot to put your
matchHeight
call in a closure. Change your last line to:K 以便该链接正在寻找指向该函数的指针。当你传递一个匿名函数(如#1)时,该函数被创建/存储在内存中,并且该位置被传入。但第二种情况,JS执行该函数(因为你的括号+参数),然后传递结果作为指针...这不是你想要的。
您需要做的是:
或者仍然使用匿名函数:
FWIW
window.addEvent
不完全跨浏览器兼容,另请参阅window.attachEventK so that link is looking for a pointer to the function. When you pass it an anonymous function (as in eg #1) that function is created/stored in memory, and that location is passed in. Your second case though, JS executes the function (because of your brackets+arguments) and then passes the result as the pointer... which isn't want you want.
What you'd need to do instead is:
Or still use an anonymous function:
FWIW
window.addEvent
isn't totally cross-browser compatible, see also window.attachEvent