forEach无法使用break和return来中断,只能使用throw catch来达到中断的效果了。

var id = (function(){
    // forEach 是无法中断的。除非用这种hack
    try {
        top.document.querySelectorAll('.layui-nav-tree a[data-options]').forEach(function (e, i) {
             var options = $(e).attr("data-options")
             if (~options.indexOf(window.location.pathname)) {     
                throw new Error(eval('(' + options + ')').id)
             }
        });
    } catch(err) {
        return err.message
    }
}());

 

相关文章:

  • 2021-09-15
  • 2021-06-01
  • 2021-06-13
  • 2021-07-12
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
猜你喜欢
  • 2021-09-17
  • 2022-12-23
  • 2021-11-05
  • 2021-07-31
  • 2021-10-15
  • 2021-08-25
  • 2021-07-12
相关资源
相似解决方案