【问题标题】:Coffeescript/Javascript loop not executingCoffeescript/Javascript 循环未执行
【发布时间】:2014-12-28 22:13:13
【问题描述】:

我有一个简单的咖啡脚本来处理特定的选择器 (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 个项目。 For 循环运行 - 但是 procBl 不打印警报(它安静地退出) 似乎正在迭代并传递给函数的 bl 不是正确的对象,我无法弄清楚这段代码有什么问题-

感谢任何帮助

【问题讨论】:

    标签: javascript jquery coffeescript


    【解决方案1】:

    bl 传入procBl 函数不是一个 jQuery 实例,而是普通的 DOM 元素。用$() 包裹它以使用children 方法:

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

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

    【讨论】:

    • dfsq -- 用 $ 包装在 for 循环中起作用,但在函数中不起作用 -- 它安静地退出 -- 你能告诉我在集合上使用 .each 的语法吗..谢谢..
    • $("[id*=part_id]").each -> alert "# of children " + $(this).children().length;
    • 谢谢 .. 正如我所说的,它在 for 循环中工作 .. 但我仍然无法使用 bl 或 $(bl) 调用该函数 :-(
    猜你喜欢
    • 2015-06-05
    • 2023-03-29
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 2011-12-18
    • 2013-04-13
    • 2012-12-08
    相关资源
    最近更新 更多