【发布时间】:2011-08-02 09:55:22
【问题描述】:
我想创建一些divs,我可以移动和调整它们的大小,并将它们的width、height 等绑定到数组中的一个对象。所以,如果我创建六个 div,我的数组中有六个对象,每个对象都有 .width、.height 等。
我不太明白如何使用 knockout.js 将输入和跨度文本绑定到数组对象属性。这是我的尝试:
var counter = 0;
var objects = [];
$(document).ready(function () {
dostuff($("#main")); // give it a target container div
});
function dostuff(target) {
counter++;
// create a div containing a span and an input that binds to knockout.js
target.append('<div id="d' + counter + '">width:<span id="d' + counter +
'" data-bind="text:objects[' + counter + '].width"></span>' +
'<input type="text" id="d' + counter +
'" data-bind="value:objects[' + counter + '].width"/></div>');
var a = $("#d" + counter);
a.css("position", "absolute");
a.css("width", "100px");
a.css("height", "100px");
a.css("background-color", "#" +
Math.ceil(Math.random()*9) + "0" +
Math.ceil(Math.random()*9) + "0" +
Math.ceil(Math.random()*9) + "0");
a.resizable({
stop: function (e, ui) {
this.childNodes[2].value = ui.size.width;
}
});
objects[counter] = { width: "100px", height: "100px",
top: "0px", left: "0px" };
ko.applyBindings(objects[counter]);
}
如何让 objects[1].width 绑定到 div d1 的 <input> 值?
【问题讨论】:
标签: javascript jquery jquery-ui knockout.js