js怎样获取 鼠标落下事件(onmousedown)所点击的div的值 加入有若干个 class="idDrag"的div 
这样的思路 
1.首先判断鼠标落下的标签的 class是否是="idDrag", 
2.如果是,则得到该div的id 值 或者 
为class为idDrag的元素 绑定onmousedown事件 
点击其中一个div时 得到这个div的 id 我是这样写的但是不对//相当于getelementsByClassName()方法   
var $js = function(classStr) {   
    var el = [],   
        _el = document.getElementsByTagName('*');   
    for(var i=0;i<_el.length;i++){   
      if(_el[i].className == classStr){   
        el[el.length] = _el[i];   
      }   
    }   
    return el;   
};   
  
window.onload = function() {   
    var objs = $js("idDrag");   
    for(var i=0;i<objs.length;i++) {   
      addEventHandler(objs[i], "mousedown", function(e){   
        var DragId = objs[i].id;   
      });   
      //.onmousedown =    
    }   
  };  
var DragId = objs[i].id 这里报错,因为objs[i]是数组里面有多个div 
//addEventHandler是自己封装的 不用管它,总之它可以绑定事件 大家帮忙想想办法

解决方案 »

  1.   

    用jquery的话
    $("div .idDrag").mousedown(function(){
       alert(this.id);
    });
      

  2.   


    /**
     * @see 鼠标点击拖拽的效果(页面可以同时拖动多个框)
     * @param boxId 整个对象(框)的id(一般为div或table)
     * @param event 内置对象(必须传入)
     */
    function mousePlead1(event, boxId) {
    var o = getO(boxId);
    var isIE = document.all ? true : false;
    var e = event;
    var x = e.offsetX || e.layerX;
    var y = e.offsetY || e.layerY;
    document.onmousemove = function(e) {
    o.style.filter = 'Alpha(opacity=70)';
    o.style.opacity = '0.7';
    if (isIE) {
    o.setCapture();
    } else {
    window.captureEvents(Event.MOUSEMOVE);
    }
    var e = window.event || e;
    if (e.clientX - x >= 0 && e.clientY - y >= 0 && e.clientX - x <= getWinSize()[0] - getO(boxId).offsetWidth
    && e.clientY - y <= getWinSize()[1] - getO(boxId).offsetHeight) {
    o.style.left = (e.clientX - x) + "px";
    o.style.top = (e.clientY - y) + "px";
    }
    };
    document.onmouseup = function(e) {
    document.onmousemove = function() {
    };
    if (isIE) {
    o.releaseCapture();
    } else {
    window.releaseEvents(o.MOUSEMOVE);
    }
    o.style.filter = "";
    o.style.opacity = "";
    };
    }/**
    * @see 获得对象
    * @param id 对象的id(表单元素和其他标签都可以)
    * @return Object
    */
    function getO(id) {
    return document.getElementById(id);
    }/**
     * @see 获得当前窗体的大小(width,height)
     * @return Array 
     */
    function getWinSize() {
    var width = parseInt(document.documentElement.clientWidth);
    var height = parseInt(document.documentElement.clientHeight);
    return new Array(width, height);
    }
    <div id="divUsers" class="floatDiv" style="border:#FFCCFF 2px solid;left:99px;top:39px;">
        <table width="150" cellpadding="0" cellspacing="0">
         <tr>
          <td class="title_bg1" width="90%" align="left" style="padding-left:5px;cursor:move;" onmousedown="mousePlead1(event,'divUsers')">
           <strong><font color="#FF33FF">选择接收人</font></strong>
          </td>
          <td class="title_bg1" width="10%" align="right" style="padding-right:2px;padding-top:3px;" height="20">
           <img src="<z:contextPath/>/common_res/images/check/dialogclose.gif" 
           style="cursor:pointer;border:0px;" title="关闭" onclick="showDiv('divUsers','0');showDiv('imgflash0','1')"/>
          </td>
         </tr>
         <tr>
          <td colspan="2" bgcolor="#FFFFFF" valign="top">
           <div class="divScroll" style="width:150px;height:165px;">
            <z:not-null name="users">
             <div style="line-height:20px;padding-left:5px;" align="left">
              <input type="checkbox" id="ckbUser0" name="allUsers" onclick="chooseUser('0','所有人')" 
              value="0" style="width:auto;"/>所有人
             </div>
             <z:for name="users" id="u">
              <div style="line-height:20px;padding-left:5px;" align="left">
               <input type="checkbox" id="ckbUser<z:print name="u.id"/>" name="allUsers" onclick="chooseUser('<z:print name="u.id"/>','<z:print name="u.realName"/>')" 
               value="<z:print name="u.id"/>" style="width:auto;"/><z:print name="u.realName"/>
              </div>
             </z:for>
            </z:not-null>
           </div>
          </td>
         </tr> 
        </table>
       </div>
      

  3.   

    可以换一种思路来实现,你动态的给class为idDrag加上一个onmousedown的事件,并且调用一个方法,同时把这个对象出入到方法里面去,这样就可以获取到ID了。给你一个我写过的例子,你修改一下就可以了function getElementsByClassName(className){
    var docs = [];
    allElements = document.getElementsByTagName('*');
    for(var i = 0; i < allElements.length; i++){
    if(allElements[i].className == className){
    docs[i] = allElements[i];
    }
    }
    return docs;
    }
    function initOnkeyPressAction(className,functionName){
    var docs = getElementsByClassName(className);
    for(var i = 0; i < docs.length; i++){
    if(docs[i] != null){
            //这里我绑定的是onkeypress事件而已
    docs[i].onkeypress = eval(functionName);
    docs[i].onpaste = function(){return false;};
    docs[i].oncontextmenu = function(){return false;};
    docs[i].oncopy = function(){return false;};
    docs[i].style.imeMode = "disabled";
    }
    }
    }
    initOnkeyPressAction("number","pressNumber");
      

  4.   

    谢谢各位 我解决了
    document.onmousedown = function(e) {
        var oeve = e || event;
        if(isIE) {
          var id = oeve.srcElement.id;
        } else {
          var id = oeve.target.id;
        }