通过不限制多少元素来检查

时间:2014-08-14 01:32:02

标签: javascript

我想让它成为例如当单词溢出元素时,检查部分不会仅限于四个。(在代码中将其注释为"此部分")。我希望依赖于分割的元素数量,以便它是正确的。

 if(original[0]==$("#box1").text()&&original[1]==$("#box2").text()&&original[2]==$("#box3").text()&&original[3]==$("#box4").text())

这仅限于四个,然后检查将是正确的。我怎么能这样做,例如将有三个分裂元素或多于四个元素,并且检查仍然是正确的。

<script>
js_var = "<?php echo $output ?>";
var c = ['c1','c2','c3','c4','c5','c6','c7','c8','c9','c10'];
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("#");


var x = document.getElementById("demo");
for (var i=0; i < original.length; i++) {
var el = document.createElement('div');
el.id = c[i];
el.innerHTML = original[i];
x.appendChild(el);
}


function getNumbers() {
var player1 = new Array();
balls90.sort(function() {
    return Math.random() - .25;
    document.getElementById("shuffle").onclick = function(){displayDate()};
});

 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]);


    document.writeln('<div id='+div_id[i]+' draggable=true droppable=true 
ondragstart=drag(event)>'+balls90[i]+'</div>');

 }

}

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));

 //this part

if(original[0]==$("#box1").text()&&original[1]==$("#box2").text()&&original[2]==$("#box3").text()&&original[3]==$("#box4").text())
    {
        alert('correct');
    }

}
<script>

1 个答案:

答案 0 :(得分:0)

这些方面的东西:

var result = true;
for (var i=0, i<original.length, ++i) {
   if (original[i] !== $("#box"+(i+1)).text()) {
     result = false;
     break;
   }
}

if(result) {
    alert('correct');
}