【发布时间】:2014-06-26 15:35:38
【问题描述】:
我正在努力尝试使用 coffeeScript 和 google 闭包库将表、行和列注入 HTML 页面。如果我不能在 coffeeScript 中完成它,那么我想我可以直接恢复到 jQuery 或 javaScript,所以也欢迎在代码中提供一些方向。
文档对 goog 闭包库的帮助不是很大,而且我对 coffeeScript 比较陌生。谷歌闭包库的一半命令也被弃用或神秘。
任何帮助将不胜感激,以下是当前的 HTML,所需的 HTML,然后是咖啡脚本。
这里是 HTML:
<div id="propertiesContainer" class="propertiesContainer" style="visibility: visible; left: 954.7777862548828px; top: 50.989585876464844px;">
<div class="propertyEditor">
<label>label</label>
<input type="text">
</div>
<div class="propertyEditor">
<label>size</label>
<input type="text">
</div>
</div>
我希望 HTML 看起来像:
<div id="propertiesContainer" class="propertiesContainer" style="visibility: visible; left: 954.7777862548828px; top: 50.989585876464844px;">
<div class="propertyEditor">
<table>
<tr>
<td><label>label</label></td>
<td><input type="text"></td>
</tr>
</table>
</div>
<div class="propertyEditor">
<table>
<tr>
<td><label>size</label></td>
<td><input type="text"></td>
</tr>
</table>
</div>
</div>
coffeeScript 目前看起来像:
render: (parent) ->
@title = goog.dom.createDom 'label', null, goog.dom.createTextNode(@name)
@input = goog.dom.createDom 'input', {'type': 'text'}, null
container = goog.dom.createDom 'div', {'class': 'propertyEditor'}, @title, @input
goog.dom.appendChild parent, container
return
【问题讨论】:
-
然后清除所有无效的嵌入 div
-
好的,HTML 现在应该是有效的,现在有什么建议吗?
-
我是否正确假设 div
propertiesContainer是parent?如果是,那么编写的代码将创建类propertyEditor的 div,并在将整个内容附加到父级之前添加一个<label>和一个<input>节点。哪个是您称为“当前 HTML”的输出 - 如果您希望获得表格,则需要更改咖啡脚本代码以创建它们。如果我做了不正确的假设,请告诉我。
标签: javascript jquery html coffeescript google-closure