我有以下功能:
$('#cover-drop-zone')[0].ondrop = function(e) {
如何使用上述代码定位两个不同的div?我希望两个div都有ondrop方法。
答案 0 :(得分:2)
为什么不使用jQuery:
$('#cover-drop-zone, #other-element-id').on('drop', function(e){
// do stuff here
});
由于您的问题未使用jquery标记,因此您可以使用纯JavaScript:
var elems = document.querySelectorAll('#cover-drop-zone, #other-element-id');
function dropFunction (e) {
// e is the event (passed automatically as the first parameter,
// this will refer to the element on which the function is called
// and whatever your function would/should do...
}
for (var i = 0, len = elems.length; i < len; i++){
elems[i].addEventListener('drop', dropFunction);
}
参考文献:
on()
。答案 1 :(得分:0)
使用此代码:
$('#cover-drop-zone, #div2').bind('ondrop ', function(e) { })