获取当前元素

问题来源:一个页面中有许多个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;
 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值