为什么单独定义的函数不能在“domready”上工作? JavaScript 中的事件?

发布于 2024-11-08 07:23:30 字数 1431 浏览 2 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-11-15 07:23:30

这是因为你需要删除括号;函数是一个对象,您需要传递函数,而不是它的返回值。因此,对于第一个示例,您应该:

window.addEvent('domready', function() { matchHeight('div#leftcolumn div.module_menu'); } );

相反。对于第二个示例,它是:

function giveMessage() { alert('test'); }
window.addEvent( 'domready', giveMessage)

同样,您传递对象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:

window.addEvent('domready', function() { matchHeight('div#leftcolumn div.module_menu'); } );

Instead. For your second example, it's:

function giveMessage() { alert('test'); }
window.addEvent( 'domready', giveMessage)

Again, you pass the object giveMessage, not the result obtained after invoking it.

郁金香雨 2024-11-15 07:23:30

您忘记将 matchHeight 调用放入闭包中。将最后一行更改为:

window.addEvent( 'domready', function(){matchHeight( 'div#leftcolumn div.module_menu' )} );

You forgot to put your matchHeight call in a closure. Change your last line to:

window.addEvent( 'domready', function(){matchHeight( 'div#leftcolumn div.module_menu' )} );
难如初 2024-11-15 07:23:30

K 以便该链接正在寻找指向该函数的指针。当你传递一个匿名函数(如#1)时,该函数被创建/存储在内存中,并且该位置被传入。但第二种情况,JS执行该函数(因为你的括号+参数),然后传递结果作为指针...这不是你想要的。

您需要做的是:

function setup() {
  matchHeight( 'div#leftcolumn div.module_menu');
}
window.addEvent('domready',setup);

或者仍然使用匿名函数:

window.addEvent('domready',
  function() {matchHeight( 'div#leftcolumn div.module_menu');});

FWIW window.addEvent 不完全跨浏览器兼容,另请参阅window.attachEvent

K 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:

function setup() {
  matchHeight( 'div#leftcolumn div.module_menu');
}
window.addEvent('domready',setup);

Or still use an anonymous function:

window.addEvent('domready',
  function() {matchHeight( 'div#leftcolumn div.module_menu');});

FWIW window.addEvent isn't totally cross-browser compatible, see also window.attachEvent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文