js實現div拖拽互換位置效果


可以實現div拖拽互換位置,可以是多個div,div中放上img還是挺有用的

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <title></title>   <style type="text/css"> #div1, #div2 { float: left; width: 100px; height: 35px; margin: 10px; padding: 10px; border: 1px solid #ccc; line-height:35px; } </style> <script type="text/javascript"> function allowDrop(ev) { ev.preventDefault(); } var srcdiv = null; var temp = null; //當拖動時觸發 function drag(ev, divdom) { srcdiv = divdom; temp = divdom.innerHTML; } //當拖動完后觸發 function drop(ev, divdom) { ev.preventDefault(); if (srcdiv !== divdom) { srcdiv.innerHTML = divdom.innerHTML; divdom.innerHTML = temp; } } </script> </head> <body>     <div id="div1" ondrop="drop(event,this)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event, this)" style="">         <p>我是第一!</p>     </div>     <div id="div2" ondrop="drop(event,this)" ondragover="allowDrop(event)" draggable="true" ondragstart="drag(event, this)" style="">         <p>那我第二。</p>     </div> </body> </html> 

文章轉自這里


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM