只将一个移动器放在一个盒子里

时间:2014-07-24 06:13:41

标签: javascript php html5

我想限制盒子里只有一个移动器。但是,下面的代码显示,在一个盒子中,它可以放置多于一个我不希望发生这种情况的移动器。我可以知道如何在一个盒子里只制作一个移动器吗?非常感谢!:)

<body>
<div id="cardContainer" draggable="true"></div>
<style>
.box {width:200px;height:20px;padding:10px;border:1px solid #aaaaaa;display: inline-block;}
.mover{color:red; background-color: yellow; width:auto ; font-size: 30px ; border: 2px}
#id1 {color:red; background-color: yellow; width:350px ; font-size: 20px ; border: 2px}
</style>

<div id>Is this draggable and droppable?</div>

</body>

<head>
<?php
$conn = mysql_connect("localhost", "root", "password");


if (!mysql_select_db("login")) {
echo "Unable to select go database " . mysql_error();
exit;
}

$sql = "SELECT table
    FROM   se
    WHERE  id = 1";

$result = mysql_query($sql);


while ($row = mysql_fetch_assoc($result)) {
$output =  $row["full"];

}

mysql_free_result($result);

?>

<script>
var js_var = "<?php echo $output ?>";
var div_id = ['id1','id2','id3','id4','id5','id6','id7','id8','id9','id10'];
var box_id = ['box1','box2','box3','box4','box5','box6','box7','box8','box9','box10'];
var original=js_var.split("#");
var balls90= js_var.split("#");

function getNumbers() {
var player1 = new Array();
balls90.sort(function() {
    return Math.random() - .25;
});

 for (var i = 0; i < balls90.length; i++) {
     document.writeln('<div id="+box_id[i]+" class="box" droppable="true" ondrop="drop(event)"         
ondragover="allowDrop(event)"></div>');
 }


for (var i = 0; i < balls90.length; i++) {
    player1.push(balls90[i]);

}

}

getNumbers();

function dragWord(dragEvent){
dragEvent.dataTransfer.setData("text/html",         
dragEvent.target.textContent+"|"+dragEvent.target.parentNode.id);
}


function dropWord(dropEvent){ 
var dropData = dropEvent.dataTransfer.getData("text/html"); 
var dropItems = dropData.split("|"); 
var prevElem = document.getElementById(dropItems[1]); 
prevElem.getElementsByTagName("div")[0].textContent = dropEvent.target.textContent; 
dropEvent.target.textContent = dropItems[0]; 
dropEvent.preventDefault(); 
} 

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));
     //var data_content=document.getElementById(data).innerText;
    //alert('The selected text is: ' + data_content + '.');
}




</script>
</head>

1 个答案:

答案 0 :(得分:0)

使用 if loop 而不是 for loop

 if(var i = 0) {
         document.writeln('<div id="+box_id[i]+" class="box" droppable="true" ondrop="drop(event)"         
    ondragover="allowDrop(event)"></div>');
     }


if(i==o){

      player1.push(balls90[i]);

}