mousedown后没有激活mouseup。我不确定为什么。但如果我取消注释该行
$(this).replaceWith("<div class=" + "'" + $canvas.replace(".", " ") + "'" + "></div>");
然后mouseup正常工作。这是一段代码
var onmousemove = function ($canvas) {
// Painting the map with canvas
$map_canvas.mousedown(function () {
var ignoreMouseMove = false;
mouseDownFired = true;
$(".map .row div").mousemove(function (e) {
if (ignoreMouseMove) return;
//$(this).replaceWith("<div class=" + "'" + $canvas.replace(".", " ") + "'" + "></div>");
moveCanvas($canvas, e)
}).mouseup(function () {
ignoreMouseMove = true;
});
});
var paletteClone = $(document).clone(true); // bound handler for .palette div
};
var moveCanvas = function ($canvas, e) {
$(".map ul li." + $canvas).offset({
left: e.pageX - 30,
top: e.pageY - 30
});
};
答案 0 :(得分:1)
您正在将mouseup
事件绑定到DOM元素,然后在mousemove
事件中将其替换 - mouseup
事件无法运行,因为该元素不再出现在页面上。
另一种方法是使用mouseup
函数使用on
的父项绑定.map .row
事件 - 这会将事件绑定到父级,因此添加的元素仍会获得行为你期待。
var onmousemove = function ($canvas) {
// Painting the map with canvas
$map_canvas.mousedown(function () {
var ignoreMouseMove = false;
mouseDownFired = true;
$(".map .row").on("mousemove", "div", (function (e) {
if (ignoreMouseMove) return;
// this is now OK - event bound to parent
$(this).replaceWith("<div class=" + "'" + $canvas.replace(".", " ") + "'" + "></div>");
moveCanvas($canvas, e)
}).on("mouseup", "div", function () {
ignoreMouseMove = true;
});
});
var paletteClone = $(document).clone(true); // bound handler for .palette div
};
var moveCanvas = function ($canvas, e) {
$(".map ul li." + $canvas).offset({
left: e.pageX - 30,
top: e.pageY - 30
});
};