Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript js通过页面上的div循环并生成按钮。咖啡脚本_Javascript_Html_Knockout.js_Coffeescript - Fatal编程技术网

Javascript js通过页面上的div循环并生成按钮。咖啡脚本

Javascript js通过页面上的div循环并生成按钮。咖啡脚本,javascript,html,knockout.js,coffeescript,Javascript,Html,Knockout.js,Coffeescript,我试图循环浏览页面上的所有div,找到所有具有相同类的div:item,并将它们变成一个按钮 以下是html中的数据示例: <div class='item'> <span><a class='test' href='http://google.com'>tester1</a></span> </div> <div class='item'&

我试图循环浏览页面上的所有div,找到所有具有相同类的div:item,并将它们变成一个按钮

以下是html中的数据示例:

         <div class='item'>
          <span><a class='test' href='http://google.com'>tester1</a></span>
          </div>
          <div class='item'>
          <span><a class='test' href='http://google.com'>tester2</a></span>
          </div>
          <div class='item'>
          <span><a class='test' href='http://google.com'>tester</a></span>
          </div>
          <div class='item'>
          <span><a class='test' href='http://google.com'>test</a></span>
          </div>

我不确定如何继续,有什么建议可以让我走上正确的方向吗?

我不确定你是否有足够的资源来使用Knockout来完成这项工作。也许把小提琴放在一起

不过,您可以使用jQuery实现这一点,然后在需要时让Knockout知道事情已经发生了变化

$('.item').each ->
 if $(this).hasClass 'buttonClass'
  // turn $(this) into a button and manipulate in other ways
 else
 // ad a class or something that lets you find these in the future perhaps?

您不是在使用
RequireJS
?这个问题也应该加上这样的标签。从未使用过Coffeescript,但当我将你的Cs与常规JS进行比较时,你应该首先修复缩进,这对Cs来说似乎很重要,然后阅读,可能还学习OO-JS概念。有什么原因不能使用jQuery选择器,然后对返回的每个元素将其转换为按钮?e、 g.
$(“.item”)。每个(函数(){…转到按钮…})
数据来自哪里?它是硬编码的吗?如果你想使用knockout,你很可能不会在DOM中搜索div。如果您想为此使用knockout,您可能首先会让knockout创建div,并使用bindingHandler将其制作成一个按钮(但是,为什么不在制作时也将其制作成一个实际的HTML按钮呢?)。您询问(在DOM中搜索)的方式更多的是一种jQuery方式,而不是击倒方式。对于jQuery,请查看上面@Jarga的评论。
$('.item').each ->
 if $(this).hasClass 'buttonClass'
  // turn $(this) into a button and manipulate in other ways
 else
 // ad a class or something that lets you find these in the future perhaps?