我想創建一個可以移動和調整大小的div
,並將它們的width
,height
等綁定到數組中的對象。所以,如果我創建六個div,我的陣列中有六個對象,每個對象都有.width
,.height
等。如何綁定div寬度/高度以形成字段?
我不太瞭解如何將輸入和跨度文本綁定到數組對象屬性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>
價值?
下面是綁定的一種快速示例,其中div通過'style'綁定綁定,用於設置對象的值並使用'event'綁定通過拖動或調整大小來跟蹤更新。 http://jsfiddle.net/rniemeyer/a6Gjs/ – 2011-04-10 19:27:54
你的答案是一個敲出公園的答案。我真的很感激。 – 2011-04-11 02:38:14
大聲笑沒有雙關語意。 – 2011-04-11 03:23:21