在draganddrop中添加多个图像

时间:2013-10-03 12:06:11

标签: javascript html

    <!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
#divdrag1,#divdrag2
{float:left; width:100px; height:200px; margin:10px;padding:10px;border:1px solid #aaaaaa;}

 #div2,#div3
{float:left; width:200px; height:100px; margin:10px;padding:10px;border:1px solid #aaaaaa;}


</style>
<script>

function allowDrop(ev)
{
ev.preventDefault();
}

function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}

function drop(ev)
{
ev.preventDefault();
var data=ev.dataTransfer.getData("Text");
ev.target.appendChild(document.getElementById(data));
document.getElementById(data).width="200"
document.getElementById(data).height="100"
}
</script>
</head>
<body>
<form id="f1" name="form1" method="post">



    <div name="answer">
        <div id="divdrag1" ondrop="drop(event)" ondragover="allowDrop(event)" >
            <img src="smiley.png" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
        </div>
        <div id="divdrag2" ondrop="drop(event)" ondragover="allowDrop(event)" >
            <img src="pic1.jpg" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31">
        </div>

    </div>

    <div>
        <div id="div2" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
        <div id="div3" ondrop="drop(event)" ondragover="allowDrop(event)"></div>
    </div>




</form>
</body>
</html>

嘿我需要在单个div(div name =“answer”)中添加多个图像,我试过拖拉控制,但它不会来。每个图像都显示在单独的div中。当我拖动它也没有正确显示时请告诉我我做错了什么。哪一个我必须纠正。请指导我的朋友

1 个答案:

答案 0 :(得分:0)

<div id="divdrag1" ondragover="allowDrop(event)" >
            <ul type="none">
                <li><img src="smiley.png" draggable="true" ondragstart="drag(event)" id="drag1" width="88" height="31" align="left"> </li>
                <li><img src="pic1.jpg" draggable="true" ondragstart="drag(event)" id="drag2" width="88" height="31"> </li>
            </ul>

        </div>

使用这些tad我们可以在div标签中添加多个图像