Sunday 15 January 2012

javascript - Combining if statements -


I have a simple HTML5 & amp; Javascript drag and drop game in which the user should drag a display inside the empty div into a device.

The drag and drop works correctly, and I have managed to manage the javascript code when # answer1 is dragged into the correct div, Code> #target

However, when I duplicate the I statement, it alerts the alert, but still the wrong information, which I suspect is that if the statement goes straight The other thing is.

Is there a way to separate the statement, then does it only run when someone draws an image, if not all the statements run? Allow the function (ev) {ev.preventDefault (); } Function drag (EV) {ev.dataTransfer.setData ("content", ev.target.id); } Function drop (ev) {ev.preventDefault (); Var image = ev.dataTransfer.getData ("content"); Ev.target.appendChild (document.getElementById (image)); If ($ ('# target1'). ('# Answer1'). Length == 1) {Warning ("right!"); } And {warnings ("wrong!"); } If ($ ('# target2'). ('# Answer2'). Length == 1) {Warning ("true!"); } And {warnings ("wrong!"); }}

JSFiddle for full webpage:

An option is to check only the target on which an image was dropped. drop () . function drop (ev) {ev.preventDefault) makes it easy to add or remove targets in your game without changing the code. ); Var image = ev.dataTransfer.getData ("content"); Ev.target.appendChild (document.getElementById (image)); Var targetnum = ev.target.id.replace ("target", ""); If ($ ('$ target' + targetnum) Find ('# answer' + targetnum). Length == 1) {alert ("right!"); } And {warnings ("wrong!"); }}

No comments:

Post a Comment