开发者

How to exit mootools each()

开发者 https://www.devze.com 2022-12-30 02:03 出处:网络
How can I exit the each function when the conditions was true once? This does not work: $$(\'.box div\').ea开发者_如何学编程ch(function(e) {

How can I exit the each function when the conditions was true once?

This does not work:

  $$('.box div').ea开发者_如何学编程ch(function(e) {
 if(e.get('html') == '') {
    e.set('html', 'test');
    exit;
 }
  });


Use .some?

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

But probably you could just use

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


Just throw something and catch it higher:

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

But using a combination of .every and .some would be a far better idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号