用JavaScript实现拖动div到另一个div,拖放后改变被拖动div的宽度及内容
时间: 2023-09-08 15:12:40
浏览: 70
可以使用JavaScript的Drag and Drop [api](https://geek.csdn.net/educolumn/2ea7fda16f533cf64692fb96e5e2546c?spm=1055.2569.3001.10083)来实现拖动div到另一个div的效果。以下是一个简单的示例[代码](https://geek.csdn.net/educolumn/1572ef9b473b4e00f6b2bf6d428b7c27?spm=1055.2569.3001.10083):
```html
<!DOCTYPE html>
<style>
#div1, #div2 {
width: 200px;
height: 100px;
padding: 10px;
border: 1px solid black;
mar[gin](https://geek.csdn.net/educolumn/1aef582107c22aa4d9f243890549bf39?spm=1055.2569.3001.10083): 10px;
</style>
</head>
<div id="div1" ondrop="drop(event)" ondragover="allowDrop(event)">
<p>拖动我</p>
<div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
<script>
function allowDrop(ev) {
ev.preventDefault();
function drag(ev) {
ev.dataTransfer.setData("text", ev.tar[get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083).id);
function drop(ev) {
ev.preventDefault();
var data = ev.dataTransfer.[get](https://geek.csdn.net/educolumn/31aebce56f4ecc5451aac3a194ff6259?spm=1055.2569.3001.10083)Data("
```