【发布时间】:2015-11-05 13:41:06
【问题描述】:
我有以下可拖动和可选择的 jQuery 代码用于项目拖动/全部选择,脚本工作正常,直到页面被更改并且 <div id="content"></div> 内的内容通过 ajax 加载,然后可拖动和可选择不识别元素,根本不起作用。
ajax 页面加载前<div id="content"></div> 的 HTML:
<div id="content">
<div class="files_">
<div class="item_clickable"><span>hello element</span></div>
<div class="item_clickable"><span>hello element</span></div>
<div class="item_clickable"><span>hello element</span></div>
</div>
</div>
从 ajax 调用加载的 HTML:
<div class="files_">
<div class="item_clickable"><span>hello element</span></div>
<div class="item_clickable"><span>hello element</span></div>
<div class="item_clickable"><span>hello element</span></div>
</div>
可拖动应用在:
.files <div class="files"></div>
页面加载代码示例:
function (event, url, manual) {
if (typeof manual === "undefined") {
manual = false;
}
if (typeof url === "undefined") {
link = $(this);
if (link.data('no-ajax') === true)
return;
var href = link.attr("href"),
target = (typeof link.data('target') !== "undefined") ? link.data('target') : '#content',
append = (typeof link.data('append') !== "undefined") ? link.data('append') : false,
changeUrl = (typeof link.data('change-url') === "undefined") ? true : link.data('change-url'),
type = (typeof link.data('type') !== "undefined") ? link.data('type') : 'GET';
if (!href || href === "#" || href === "javascript:void(0);" || href === "javascript:void(0)")
return;
console.log(changeUrl);
} else {
target = '#content';
type = "GET";
append = false;
changeUrl = true;
var href = url;
}
$.ajax({
type: type,
url: href,
async: true,
beforeSend: function () {
Buckty.loading('s');
}
}).always(function () {
Buckty.loading('h');
}).done(function (data) {
var content = $(data).filter('#content').html();
var matches = data.match(/<title>(.*?)<\/title>/);
if (matches) {
var title = matches[1];
}
if (title)
document.title = title;
if (content) {
if (append === false)
$(target).html(content);
else
$(target).append(content);
} else
$(target).html(data);
if (changeUrl) {
manualStateChange = manual;
History.pushState({}, document.title, href);
}
});
return false;
}
可拖动和可选择的脚本:
if ($(".files_").length) {
jQuery(".files_").selectable();
// manually trigger the "select" of clicked elements
jQuery(".files_ > div").click(function (e) {
if (e.metaKey == false) {
// if command key is pressed don't deselect existing elements
$(".files_ > div").removeClass("ui-selected");
$(this).addClass('ui-selected')
} else {
if ($(this).hasClass("ui-selected")) {
// remove selected class from element if already selected
$(this).removeClass("ui-selected");
} else {
// add selecting class if not
$(this).addClass("ui-selecting");
}
}
//$( ".files_" ).data("files_")._mouseStop(null);
});
// starting position of the divs
jQuery(".folder_container ul li").droppable({
accept: '.files_ .file_item',
drop: function (event, ui) {
console.log(ui);
}
});
jQuery(".files_ .file_item").draggable({
helper: drag_me,
cancel: '.uploading',
cursor: 'move',
cursorAt: {
left: 0,
top: 0
}
});
}
ajax 调用适用于任何页面或元素的每次点击,但从 ajax 调用加载的元素的 JavaScript 不会影响 JavaScript 事件。
如果是关于某种小代码,我会在成功函数中插入它,但代码很大,不仅有可拖动和可选择的,还有许多其他事件,这就是我避免插入的原因它在成功函数中
如果有一个很好的解决方案来设置一堆代码以使其即使在 ajax 页面加载后也能正常工作。
【问题讨论】:
-
你必须在你的成功函数之后创建一个回调函数。
-
@JeroenBellemans 如果是关于某种小代码,我会在成功函数中插入它,但代码很大,不仅可拖动和可选择,还有许多其他事件,就是这样为什么我要避免将它插入到成功函数中
-
阿什,可以分享一下可拖动事件绑定代码吗?看起来您必须将事件委托给父 div。
-
@azizpunjani 用可拖动事件的代码更新了我的问题
-
如果你为此创建了jsFiddle,有人回答会很有用。
标签: javascript jquery html css ajax