我想知道如何使用同一个类添加不同的id我想要做的是编辑每个可拖动的我需要的是生成一个不同的id所以我可以编辑每个draggable我的id =“monkey”我需要的是id =“monkey1”monkey2 exc
var z = 1; //value to make div overlappable
$('#addText').click(function (e) {
/** Make div draggable **/
$('<div />', {
class: 'ui-widget-content',
appendTo: '.container',
draggable: {
containment: 'parent',
start: function( event, ui ) {
$(this).css('z-index', ++z);
}
}
});
});
$(document).on("dblclick", '.text', function()
{
$(this).hide(); $(this).closest('.item').find('.edit_text').val($(this).text()).show();
});
$(document).on("click", ".edit_text", function()
{
return false;
});
$(document).on("click", function()
{
var editingText = $('.edit_text:visible');
if (editingText.length)
{
editingText.hide();
editingText.closest('.item').find('.text').text($(editingText).val()).show();
}
});
ko.bindingHandlers.draggable={
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).draggable();
}
};
var vm=function(){
var self=this;
self.items=ko.observableArray();
self.textContent = ko.observable('');
self.init=function(){
self.items([]);
}
self.remove=function(item){
console.log(item);
self.items.remove(item);
}
self.addNew = function() {
self.items.push( self.textContent() );
self.textContent('');
}
self.init();
}
ko.applyBindings(new vm());
<style>.item{
width: 200px;
height: 200px;
padding: 0.5em;
background:yellow;
z-index: 1;
display:block;
}
.edit_text
{
display: none;
}
.fix_backround
{
background-color: transparent;
}
.container {
width: 500px;
height: 500px;
border: 2px solid;
position: relative;
overflow: auto;
}
</style>
<textarea data-bind="value: textContent" Placeholder="Type text to append"></textarea>
<button data-bind="click: addNew">Generate New Div</button>
<div class="container">
<div data-bind="foreach:items" class="fix_backround">
<div id="monkey" href="#" class="item" data-bind="draggable:true,droppable:true">
<span data-bind="click:$parent.remove">[x]</span><br/><br/>
<center><span class="text" data-bind="text:$data"></span></div><input class="edit_text"/></center>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script><link rel="stylesheet"
href="http://code.jquery.com/ui/1.9.2/themes/base/jquery-ui.css" />
<script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
<link rel="stylesheet"
href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
答案 0 :(得分:0)
这是一个演示如何做到这一点的小提琴:
https://jsfiddle.net/xtrinch/etjj8xnz/
主要部分是:
ko.bindingHandlers.draggable={
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).draggable();
$(element).attr('id', 'monkey'+itemsAdded)
itemsAdded = itemsAdded + 1
}
};