gpt4 book ai didi

javascript - 使用相同的类向 div 添加不同的 id

转载 作者:行者123 更新时间:2023-11-28 02:55:39 25 4
gpt4 key购买 nike

我想知道如何使用同一个类添加不同的 id 我想做的是编辑每个单独的 draggable 我需要的是生成一个不同的 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">

最佳答案

这是一个 fiddle 演示如何做到这一点:

https://jsfiddle.net/xtrinch/etjj8xnz/

主要部分是:

ko.bindingHandlers.draggable={
init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
$(element).draggable();
$(element).attr('id', 'monkey'+itemsAdded)

itemsAdded = itemsAdded + 1
}
};

关于javascript - 使用相同的类向 div 添加不同的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37227635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com