$(document).ready(function () {
var counter = 1;
var asd = '<div class="class-to-div1">' + '<div class="class-to-div col-md-2 list">' + '<div class="add-list-item static">' + '<button id="add-div">press here to add div</button>' + '</div>' + '<div class="list-item placeholder">asdsdaljnfdasdasdf</div>' + '<div class="list-item placeholder">asdsdaljnfdasdasdf</div>' + '<div class="list-item placeholder">asdsdaljnfdasdasdf</div>' + ' <div class="list-item placeholder">asdsdaljnfdasdasdf</div>' + '</div>' + '</div>';
//console.log(asd);
$('.add-list').click(function () {
$('.container').append(asd);
$('class-to-div1').each(function (counter) {
$(this).addClass('sortable-div-' + counter);
});
$('class-to-div').each(function (counter) {
$(this).addClass('sortable-list-' + counter);
});
$('.sortable-div-' + counter).sortable({
connectWith: '.sortable-div-' + counter,
});
$('.sortable-list-' + counter).sortable({
connectWith: '.sortable-list' + counter - 1,
fixed: '.static',
placeholder: "ui-state-highlight"
});
$("#sortable").disableSelection();
$("#add-div" + counter).click(function () {
var text = prompt("what should be inserted?");
if (text === null) {
alert("write something in new div");
} else {
$(".sortable-list-" + counter).append('<div class="list-item placeholder">' + text + '</div>');
}
});
counter++;
});
});
所以这是我的代码,我按下按钮创建新的div。我想把它们拖到www.trello.com上,但它们并没有动。我认为addClass()不是我想要的功能所以我该怎么办?按钮工作正常,但最少的代码不
答案 0 :(得分:3)
如果您使用Class Selector (“.class”)
,则您的选择器不正确,前缀为.
$('.class-to-div1')
而不是
$('class-to-div1')
您与$('class-to-div')
我认为addClass()不是函数,绝对addClass()是函数
你还需要修改你.each(),回调函数为你提供索引,因为你在哪里传递counter
基本上它是索引
将其更改为,从.each()
中删除计数器参数 $('.class-to-div1').each(function() {
$(this).addClass('sortable-div-' + counter);
});