Your Ad Here
首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > .NET > C#.NET > 用Javascript实现拖曳ListBox中拖曳的功能
【标  题】:用Javascript实现拖曳ListBox中拖曳的功能
【关键字】:Javascript,ListBox
【来  源】:http://blog.csdn.net/binwind/archive/2006/07/29/998776.aspx

用Javascript实现拖曳ListBox中拖曳的功能

Your Ad Here


目的就是把一个ListBox中的条目使用Drag and Drop的方式拖到其他的地方去。因为Html里面的Select控件无法解决焦点的问题.所以不能够使用它来做ListBox.所以本人使用了只有一列的table来代替ListBox(放入一个固定大小的Div里面并且通过style的设置可以出现滚动条).需要解决的是选中的问题(因为在Drag and Drop中必须禁止选中)可以通过onselectbegin事件返回False来解决.废话不多说.看看原码就应该能够清楚

<html>

<head>
<style>
<!--
.drag{position:relative;cursor:hand}
-->
</style>
<script language="JavaScript1.2">
<!--
var dragapproved=false
var dragItem = null;
var dragValue;


document.onmousedown=mouseDown;
document.onmouseup=DropDown;
document.onmousemove = mouseMove;
//-->

function mouseDown()
{
 if (event.srcElement.className=="dragArea")
 {
  if(dragItem != null)
  {
   dragItem.bgColor = "";
  }
  
  dragItem = event.srcElement;
  dragItem.bgColor="yellow";
  dragValue = dragItem.innerHTML;
  dragapproved = true;
  DropIn.style.backgroundColor = "yellow";
  
 }
 else
 {
  dragapproved = false;
  DropIn.style.backgroundColor = "";
 }
}

function DropDown()
{
 if (event.button==1&&dragapproved)
 {
  if(event.srcElement.className=="dropArea")
  {
   if(event.srcElement.innerHTML == "")
   {
    event.srcElement.innerHTML = dragValue;
   }
   else
   {
    event.srcElement.innerHTML += "<br>"+ dragValue;
   }
  }
  dragapproved=false;
  DropIn.style.backgroundColor = "";
 }
}

function mouseMove()
{
  
 if(dragapproved)
 {
  if(temp.style.display != "inline")
  {
   temp.style.display = "inline";
  }
  temp.innerHTML = dragValue;
  temp.style.pixelLeft=event.clientX;
  temp.style.pixelTop = event.clientY;
  
 }
 else
 {
  if(temp.style.display != "none")
  {
   temp.style.display = "none";
  }
 }
}
function mouseOverDropArea()
{
    if(dragapproved)
    {
        DropIn.style.backgroundColor = "red";
    }
    else
    {
       DropIn.style.backgroundColor = "";
    }
}
function mouseOutDropArea()
{
    if(dragapproved)
    {
        DropIn.style.backgroundColor = "yellow";
    }
    else
    {
        DropIn.style.backgroundColor = "";
    }
}
</script>
</head>

<body>

<p> 

<div style="overflow:auto; width:150px; height:100px" onselectstart="return false;">
<table onselectstart="return false;" width=100%>
<tr>
<td class="dragArea" id="aaab">aa
</td>
<td class="dragArea">
</tr>
<tr>
<td class="dragArea">bb
</td>
<td>
</tr>
<tr>
<td class="dragArea">cc
</td>
<td>
</tr>
<tr>
<td class="dragArea">cc
</td>
<td>
</tr>
<tr>
<td class="dragArea">ee
</td>
<td>
</tr>
<tr>
<td class="dragArea">ff
</td>
<td>
</tr>
<tr>
<td class="dragArea">gg
</td>
<td>
</tr>
</table>
</div>
</p>
<div class="dropArea" style="overflow:auto; width:150px; height:100px;border: 1px dotted #808080;" onmouseover="mouseOverDropArea();" id="DropIn" onmouseout="mouseOutDropArea();">
drop here
</div>

<div id="temp" style="border: 1px dotted #808080; background-color: #FFFFFF;display:none;position: absolute; z-index: 2;" onselectstart="return false;">aaaaa </div>
</body>

</html> 

使用WinCVS创建仓库:【上一篇】
socket异步处理问题:【下一篇】
【相关文章】
  • 在CSS中调用JAVASCRIPT
  • javascript光标聚焦取消按钮
  • javascript实现Observer模式来管理多个事件回调
  • 收集整理了一套JavaScript(Not JScript)手册,分享给大家
  • javascript 正则表达式中的特殊字符
  • ASP VBScript + Javascript 以字符串方式二级联显示文件夹
  • asp.net中在前台用js增删ListBox的items
  • 判断浏览器是否支持JavaScript和Cookies
  • javascript的一些常用正则表达式
  • javascript--第二天
  • 【随机文章】
  • tiles组件配置方法
  • 有朋友知道怎么进行劳动仲裁吗?(上海地区)
  • UNION 和UNION ALL 的区别
  • PS视频教程:阵列技巧(1)
  • 系统服务描述符表
  • 配置广域网中的Windows NT
  • 我的第一个2.6内核模块的实验
  • 小谈网络游戏同步(ZT)
  • 体验jibx的灵活和快速
  • 免费空间集合
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 bbb软讯网络 All Rigths Reserved.