我有一个简单的coffeescript来处理特定的选择器(part_ids)..

 alert "Starting  blines  " + $("[id*=part_id]").length
 for bl in $("[id*=part_id]")
   do(bl) ->
     procBl bl

  procBl  = (bl)  ->
     alert "# of children "  + bl.children().length


第一警报显示2项目在页面上。
对于循环运行-但是procBl不会打印警报(它会悄悄退出)
似乎被迭代并传递给函数的bl不是正确的对象,我无法弄清楚这是什么和此代码的错误-

任何帮助表示赞赏

最佳答案

传递给bl函数的procBl不是jQuery实例,而是纯DOM元素。用$()包裹起来使用children方法:

  procBl  = (bl)  ->
     alert "# of children "  + $(bl).children().length


...或使用$.fn.each方法遍历$("[id*=part_id]")集合。

关于javascript - Coffeescript/Javascript循环未执行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27677080/

10-11 05:19