使用jQuery函数w / Function Argument

时间:2016-07-15 13:44:13

标签: javascript jquery coffeescript underscore.js

给定一个HTML,包括jQuery,下划线和以下JS:

JS

$ cat src/test.coffee
$ ->
  rows     = $('tr')
  filtered = rowsWithChildren rows
  console.log filtered

rowsWithChildren = (rows) ->
  _.filter rows, (r) -> hasChildren r

hasChildren = (row) ->
  row.children().length == 1

HTML

$ cat test.html 
<!DOCTYPE html>
<html>

<head>
<title>test</title>
</head>

<body>

<table>
 <tr class='foo'><td>one</td></tr>
 <tr class='foo'><td>two</td></tr>
 <tr class='foo'><td>three</td></tr>
</table>

  <script type="text/javascript" src="http://underscorejs.org/underscore.js"></script>
  <script type="text/javascript" src="https://code.jquery.com/jquery-3.1.0.js"></script>
  <script type="text/javascript" src="./lib/test.js"></script>

</body>

</html>

然后,我编译了test.coffee代码:

$coffee --compile --output lib/ src/
$

我的浏览器Console在打开页面时显示以下错误:

jQuery.Deferred exception: row.children is not a function TypeError: row.children is not a function
    at hasChildren (file:///home/kevin/src/javascript_sandbox/jquery_function/lib/test.js:19:16)
    at file:///home/kevin/src/javascript_sandbox/jquery_function/lib/test.js:14:14
    at http://underscorejs.org/underscore.js:227:11
    at Function._.each._.forEach (http://underscorejs.org/underscore.js:153:9)
    at Function._.filter._.select (http://underscorejs.org/underscore.js:226:7)
    at rowsWithChildren (file:///home/kevin/src/javascript_sandbox/jquery_function/lib/test.js:13:14)
    at HTMLDocument.<anonymous> (file:///home/kevin/src/javascript_sandbox/jquery_function/lib/test.js:8:16)
    at mightThrow (https://code.jquery.com/jquery-3.1.0.js:3508:29)
    at process (https://code.jquery.com/jquery-3.1.0.js:3576:12) undefined
jquery-3.1.0.js:3793 Uncaught TypeError: row.children is not a function

如何解决此错误?

1 个答案:

答案 0 :(得分:0)

(我没有足够的声誉来评论,这实际上不是答案,而是评论。)

您的jQuery源似乎有错误。你真的把它包括在内吗?似乎在这里工作得很好:http://fiddlesalad.com/coffeescript/