英文:
Element does not being draggable after page loaded
问题
当页面加载时,我创建了一些可拖动的对象。这是代码:
let toolbar_items = document.getElementById('toolbar-items');
for (let i = 0; i < items.length; i++) {
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[i].item_id}_num0"> ${items[i].item_name} </div>
`;
let current = toolbar_items.innerHTML;
toolbar_items.innerHTML = current + htmlString;
if (i + 1 % 2 == 0) {
toolbar_items.innerHTML += "<br>";
}
};
但是这是问题所在,我在 jQuery 事件中添加了另一个可拖动的项目,但它不起作用。我已经尝试了几个小时,但是没有解决。
这是我添加新元素的事件代码:
$('.toolbar-item').on("mousedown", function(e) {
if ($(this).hasClass("toolbar-item-out")) return;
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
// 我创建了与点击的相同的元素(更改了id),然后放置在上述代码的相同位置。
// 然后在下面的代码中,我将原始元素移到我的鼠标位置。
// 原始元素是可拖动的,所以我可以将其拖动到任何位置。
// 但是新生成的元素无法拖动。
$(this).prependTo('.area').attr('style', 'position: fixed !important; color: red;').offset({ top: e.pageY, left: e.pageX }).addClass("toolbar-item-out").addClass('z-index-10')
.on( "mouseup", function() {
$(this).removeClass('z-index-10');
});
});
英文:
When page loads, I'm creating some objects which are draggable. This is the code:
let toolbar_items = document.getElementById('toolbar-items');
for (let i = 0; i < items.length; i++) {
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[i].item_id}_num0"> ${items[i].item_name} </div>
`;
let current = toolbar_items.innerHTML;
toolbar_items.innerHTML = current + htmlString;
if (i + 1 % 2 == 0) {
toolbar_items.innerHTML += "<br>";
}
};
And this works completely fine. But this is the issue, I'm appending another draggable item in an jQuery event. That doesn't work. I'm trying to figure it out for like couple hours but I couldn't.
This is the code of event which I'm appending new element:
$(`.toolbar-item`).on( "mousedown", function(e) {
if ($(this).hasClass("toolbar-item-out")) return;
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
// I created the same element (which is clicked on it) with changing the id then placed in the
// same position at the upper code.
// And at the code in the down, I'm bringing the original element to my mouse position.
// The original element is draggable so I can drag it anywhere.
// But the new generated element doesn't able to drag.
$(this).prependTo('.area').attr('style', 'position: fixed !important; color: red;').offset({ top: e.pageY, left: e.pageX }).addClass("toolbar-item-out").addClass('z-index-10')
.on( "mouseup", function() {
$(this).removeClass('z-index-10');
});
});
I need help about this.
答案1
得分: 0
好的,以下是您要的翻译部分:
这是因为脚本正在加载,当我附加任何元素时,脚本已经加载,所以元素看不到脚本。
我是这样解决的:
首先,在我创建和附加新元素的地方,我添加了2行代码:
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
// 我添加了这两行代码
let new_id = `i${items[item_id].item_id}_num${item_num + 1}`;
$(`#${new_id}`).draggable();
现在我可以拖动新生成的项目。但仍然存在一个问题。当我拖动新生成的项目时,不会出现新项目。因为-就像我说的-脚本已经加载。
所以。
我将整个代码放入一个函数中,然后在附加元素后调用它。
这是最终的代码:
function reload_event() {
$(`.toolbar-item`).on( "mousedown", function(e) {
if ($(this).hasClass("toolbar-item-out")) return;
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
let new_id = `i${items[item_id].item_id}_num${item_num + 1}`;
$(`#${new_id}`).draggable();
reload_event();
$(this).prependTo('.area').attr('style', 'position: fixed !important; color: red;').offset({ top: e.pageY, left: e.pageX }).addClass("toolbar-item-out").addClass('z-index-10')
.on( "mouseup", function() {
$(this).removeClass('z-index-10');
});
});
}
reload_event();
这个代码完美运行。
英文:
Ok. So...
This happens because scripts being loaded and when I append any element script has been already loaded so elements doesn't see the scripts.
This is how did I solved it:
First of all, at the where I create and append the new element I added 2 lines of codes:
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
// I ADDED THESE TWO CODES
let new_id = `i${items[item_id].item_id}_num${item_num + 1}`;
$(`#${new_id}`).draggable();
Now I can drag the new generated item. But there still a problem. When I drag the new generated item, there will not be any new item. Because -like I said- script is already loaded.
SO.
I put the whole code in a function then I called it after I append the element.
This is the final code:
function reload_event() {
$(`.toolbar-item`).on( "mousedown", function(e) {
if ($(this).hasClass("toolbar-item-out")) return;
let id = $(this).attr('id');
let item_id = id.split('_')[0].split('i')[1];
let item_num = id.split('_')[1].split('num')[1];
let htmlString = `
<div class="toolbar-item draggable ui-widget-content" id="i${items[item_id].item_id}_num${item_num + 1}"> ${items[item_id].item_name} </div>
`;
$(htmlString).insertBefore($(`#${id}`));
let new_id = `i${items[item_id].item_id}_num${item_num + 1}`;
$(`#${new_id}`).draggable();
reload_event();
$(this).prependTo('.area').attr('style', 'position: fixed !important; color: red;').offset({ top: e.pageY, left: e.pageX }).addClass("toolbar-item-out").addClass('z-index-10')
.on( "mouseup", function() {
$(this).removeClass('z-index-10');
});
});
}
reload_event();
That works perfectly fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论