使用jquery触发器函数传递额外参数时遇到问题。额外的参数没有通过。代码:
//click event binding with button goes here
$("#b").bind({ click:
function (e, x, y) {
alert(x + " " + y); //here`s the problem, showing undefined (expecting top & left
} //offset() of dropped button
});
//here making the button draggable for drag drop functionality (applying jquery-ui)
$("#b").draggable({ helper : "clone" });
//here creating a droppable area ( here i am dropping a draggable button)
$(".droppable").live({ mouseenter: function () {
$(this).droppable({
accept: "#b", // accepting only button whose id = b
drop: function (ev, ui) {
alert("dropped"); //alert is showing means droppable works fine
var y = ui.offset.top;
var x = ui.offset.left;
ui.draggable.trigger("click", [x , y]); // here i am triggering the click event
} // for button and passing x and y as an
}); // extra parameter
}
});
答案 0 :(得分:1)
我绘制了情况,它对我来说很好(使用相同的代码)。
可拖动和可投放的元素是什么? 我的是:
<div id="b" style="width:200px;height:200px;background-color:red"></div>
<div class="droppable" style="width:400px;height:200px;background-color:green"></div>
我的建议:
在获得偏移值后添加console.log(x, y)
- 可拖动元素可能没有它们(并且值未定义)
通过以下方式更改您的点击处理程序,以便您可以同时处理用户点击和触发点击次数:
function (e, x, y) {
if(typeof x !== "undefined" && typeof y !== undefined) {
// do trigger logic
return;
}
// do click logic
}