javascript - PrototypeJS 循环不会运行

标签 javascript prototypejs

我有一个循环无法使用 Prototype + Scriptaculous 运行。它为数组中的第一个对象运行一次,然后停止。

var myMessages = new Object();
        myMessages = ['success','info','warning','error']; // define the messages types      
        function hideAllMessages()
        {
                 var messagesHeights = new Array(); // this array will store height for each

enter code here


        // This one runs just once for the first item in the array
                 var i = 0;
                 myMessages.each(function(element) {
                    alert(element);
                    messagesHeights[i] = $('.' + element).getHeight();
                    i++;
                    $$('.' + element + ' message').invoke('hide');
                 });


                 //This won't run at all===============================
                 for (var index = 0; index < myMessages.length; index++)
                 {
                        messagesHeights[index] = $('.' + myMessages[index]).getHeight();
                        $('x').hide(); 
                        //$('.' + myMessages[i]).css('top', -messagesHeights[i]); //move element outside viewport     
                 }
        }

最佳答案

我不是原型(prototype)用户,但这是我到目前为止所看到的:

$ 用于 ID。我相信您在这里需要 $$:

$$('.' + element)

这会返回一个数组,所以我认为你需要像这样的invoke():

$$('.' + element).invoke('getHeight');

此外,.each() 将索引作为第二个参数传递给回调,因此您无需维护自己的 i

myMessages.each(function(element, i) {

另外,这个:

$$('.' + element + ' message')

...似乎正在寻找带有名为 message标签的元素。我假设您想要上课。

$$('.' + element + ' .message').invoke('hide');

关于javascript - PrototypeJS 循环不会运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6914511/

相关文章:

javascript - 在 html anchor 中返回 false 不会返回 false

javascript - 使用 settimeout 停止函数重新执行一秒钟

javascript - 脚本语言中变量的内存分配是如何完成的?

javascript - 当页面完全呈现时,您如何执行 JavaScript 函数?

javascript - 如何在另一个原型(prototype)事件完成后触发原型(prototype)事件?

javascript - 在 Prototype 中绑定(bind)和触发 native 和自定义事件

javascript - 如何使用 JavaScript 从表格中获取单元格值?

javascript - Handlebars : why this code doesn't work?

javascript - Bootbox.prompt() 将焦点设置在文本字段上

ajax - 不确定如何使用 jQuery noconflict 函数