问题来源:一个页面中有许多个DIV,而且是循环遍历出来的,现在想通过mousedown事件获取当前元素,其他事件获取方法一样。
第一种写法:
1、html
<div name="box" id="box<c:out value="${item.id}"></c:out>"
title="<c:out value="${item.FName}"></c:out>"
style=" background: #acfcef;font-size: 12px;cursor: move;overflow:auto;POSITION: absolute;
TOP:<c:out value="${item.FTop}"></c:out>;
LEFT:<c:out value="${item.FLeft}"></c:out>;
WIDTH:<c:out value="${item.FWidth}"></c:out>;
HEIGHT:<c:out value="${item.FHeight}"></c:out>">
<div id="coor" style="width: 10px; height: 10px; overflow: hidden;
cursor: se-resize; position: absolute;right: 0; bottom: 0;">
</div>
<center >
<c:out value="${item.FName}"></c:out>
</center>
</div>
2、js
var $box = $("div[name='box']").mousedown(function(e) {
var offset = $(this).offset();
$box = $ (this);//当前对象
this.posix = {'x': e.pageX - offset.left, 'y': e.pageY - offset.top};//位置移动
$.extend(document, {'move': true, 'move_target': this});
}).on('mousedown', "#coor", function(e) {
var posix = {
'w': $box.width(),
'h': $box.height(),
'x': e.pageX,
'y': e.pageY
};
$.extend(document, {'move': true, 'call_down': function(e) {
$box.css({
'width': Math.max(30, e.pageX - posix.x + posix.w),
'height': Math.max(30, e.pageY - posix.y + posix.h)
});
}});
return false;
});
第二种写法:
1、在要操作的元素中添加事件οnmοusedοwn="find(this)"
2、在js中写对应的函数find(this)
//函数中的div即当前对象
function find(div) {
var tid1 = div.id;
divid = tid1.substring(3, tid1.length);
document.all.did.value = divid;
}