JavaScript实现多个div块之间相互拖放,交换位置(内容)
来源:互联网 发布:云朵网校源码 编辑:程序博客网 时间:2024/04/30 05:59
自己写的一个小页面,直接放代码。
<!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8"> <title>数据源配置:配置</title><link href="style/base.css" rel="stylesheet" type="text/css" /><style> *{ margin: 0px; padding: 0px; } #titleDiv{ width: 800px; margin: auto; text-align: center; height: 50px; margin-top: 50px; } #submit{ width: 800px; height: 50px; background-color: blue; margin:auto; text-align: center; } #content{ width: 800px; height: 200px; background-color: blue; margin:auto; } #priority{ width:200px; background-color: burlywood; height:200px; float: left; } #priorityTitle,#dataSourceTitle,#forbiddenYNTitle{ width:200px; height: 20px; margin: auto; margin-top: 50px; text-align: center; } #priorityOne,#priorityTwo,#priorityThree,#forbiddenYN1,#forbiddenYN2,#forbiddenYN3{ width:200px; height:20px; margin: auto; margin-top: 10px; text-align: center; } #dataSourceOne,#dataSourceTwo,#dataSourceThree{ width:400px; height:20px; margin: auto; margin-top: 10px; text-align: center; } #dataSource{ width:400px; height:200px; background-color: burlywood; float: left; } #forbiddenYN{ width:200px; height:200px; background-color: burlywood; float: left; } #submitInput{ width: 60px; height: 30px; margin-top: 10px; cursor: pointer; background-color: red; }</style><script type="text/javascript" src="script/jquery.js"></script><script type="text/javascript"> function allowDrop(ev) { ev.preventDefault(); } var srcdiv = null; function drag(ev,divdom) { srcdiv=divdom; ev.dataTransfer.setData("text/html",divdom.innerHTML); } function drop(ev,divdom) { ev.preventDefault(); if(srcdiv != divdom){ srcdiv.innerHTML = divdom.innerHTML; divdom.innerHTML=ev.dataTransfer.getData("text/html"); } }</script><script> $(function() { $('#submitInput').click(function() { //发送参数 var priorityOne = $("#priorityOne").html(); var priorityTwo = $("#priorityTwo").html(); var dataSourceOne = $("#dataSourceOne").html(); var dataSourceTwo = $("#dataSourceTwo").html(); var forbiddenYN1 = $("#checkboxOne").attr("checked");//通过标签的属性获取属性值,从而获取checkbox是否被选中事件,true为选中,flase代表未选中 var forbiddenYN2 = $("#checkboxTwo").attr("checked"); window.location.href="/TVListTwo/channelList1.jsp?dataSourceOne="+dataSourceOne+"&dataSourceTwo="+dataSourceTwo+"&forbiddenYN1="+forbiddenYN1+"&forbiddenYN2="+forbiddenYN2; }) })</script></head><body> <div id="titleDiv"><h3>数据源配置</h3></div> <div id="content"> <div id="priority"> <div id="priorityTitle">优先级(1为最高优先级)</div> <div id="priorityOne">1</div> <div id="priorityTwo">2</div> </div> <div id="dataSource"> <div id="dataSourceTitle">数据源(拖动位置可调整优先级)</div> <div id="dataSourceOne" ondrop="drop(event,this)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event, this)">TVSou</div> <div id="dataSourceTwo" ondrop="drop(event,this)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event, this)">KanKanTV</div> </div> <div id="forbiddenYN"> <div id="forbiddenYNTitle">是否禁用(勾选代表禁用)</div> <div id="forbiddenYN1"><input type="checkbox" name="checkboxOne" value="yes1" id="checkboxOne" /></div> <div id="forbiddenYN2"><input type="checkbox" name="checkboxTwo" value="yes2" id="checkboxTwo" /></div> </div> </div> <div id="submit"> <input type="button" value="提交" id="submitInput"> </div></body></html>
上面的代码copy运行,中间的数据源div是可以通过拖动来交换位置的,也就实现了数据源优先级间的互换。
函数说明:
ondrop: 拖到目的地放下之后要做的处理,这里自定义drop(event,this)函数,互换两个的innerHTML
ondragover:当拖动链接等有默认事件的元素时,要在ondragover事件中用ev.preventDefault()阻止默认事件。否则drop事件不会触发。
draggable:允许拖动
ondragstart:选取拖动目标要做的处理,这里保存拖动目标的innerHTML.
0 0
- JavaScript实现多个div块之间相互拖放,交换位置(内容)
- JS实现多个div块之间相互拖放,调换位置
- JS实现div块的拖放,调换位置
- javascript实现div拖放
- QTableWidget 实现整行拖放交换位置
- 实现多个div拖拽移动,交换位置功能,代码复制可运行,有交换特效
- 拖放=>实现两个div内容互换
- js实现2个select之间内容相互移动
- dataGridView 如何实现行行的拖动,即行之间相互交换位置
- js实现两个div交换位置
- UItableView 如何实现Cell之间交换位置
- UItableView 如何实现Cell之间交换位置
- 实现div可拖放
- javascript 实现折叠展现div块
- HTML5练习,实现图片在两DIV中相互拖放效果
- Javascript实现HTML5拖放
- html5拖放--15行js代码实现两个div内容互换
- javascript 创建DIV块
- 使用Python开发微信公众号
- 标识符与数据类型
- web前端进阶书单
- [hard]41. First Missing Positive
- HttpClient 教程 (一)
- JavaScript实现多个div块之间相互拖放,交换位置(内容)
- 第二周项目2-程序的多文件组织
- Python默认编码错误SyntaxError: Non-ASCII character '\xe5'之解决方法
- Linux:如何在Linux中运行Windows程序
- 第2周项目3-体验复杂度(1)两种排序算法运行时间
- Cookie深度解析
- 第二周项目2-程序的多文件组织
- Web性能优化
- MySQL存储过程