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.首先判断鼠标落下的标签的 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是自己封装的 不用管它,总之它可以绑定事件 大家帮忙想想办法
解决方案 »
- myeclipse里如何设置时间?
- Flex通过Httpservice怎么获取后台servlet传过来的数据
- Spring 的AbstractRoutingDataSource 动态转数据源错误
- 关于sql的group by。。。
- memcache
- 使用FCKEditor,如何在提交之前通过JS获取编辑的内容?
- Struts 2 如何设置隐藏标签 把一个对象Set数据集合送到网页上?
- hibernate中一个表有复合主键,其中一个主键字段是自增的,请问该如何配置自增序列!!
- 在论坛中怎样判断用户离开了呢?
- 写好了验证码的生成了怎么调用啊
- ckeditor控件在ie8下光标移动不了
- tomcat配花生壳时,路由的虚拟服务原理
$("div .idDrag").mousedown(function(){
alert(this.id);
});
/**
* @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>
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");
document.onmousedown = function(e) {
var oeve = e || event;
if(isIE) {
var id = oeve.srcElement.id;
} else {
var id = oeve.target.id;
}
}