2010-05-11 11 views
9

¿Cómo puedo salir de cada función cuando las condiciones eran verdaderas una vez?Cómo salir de mootools cada()

Esto no funciona:

$$('.box div').each(function(e) { 
if(e.get('html') == '') { 
    e.set('html', 'test'); 
    exit; 
} 
    }); 

Respuesta

14

Uso .some?

$$('.box div').some(function(e) { 
    if(e.get('html') == '') { 
     e.set('html', 'test'); 
     return true; 
    } else 
     return false; 
    }); 

Pero probablemente usted podría utilizar

arr = $$('.box div[html=""]'); 
    if (arr.length > 0) 
    arr[0].set("html", "test"); 
+0

+1 ¡Qué buena idea flippin !! (Se tomó la libertad de agregar un enlace a los documentos.) –

+0

Gracias, funciona genial :) – Billy

+0

eres un genio! – Alex

1

Sólo throw algo y catch lo más alto:

try { 
    $$('.box div').each(function(e) { 
    if(e.get('html') == '') { 
     e.set('html', 'test'); 
     throw "break"; 
    } 
    }); 
} catch (e) { 
    if(e != "break") throw e; 
} 

Pero el uso de una combinación de .every y .some sería una idea mucho mejor.