javascript - Value of a Drag and Drop with the click of a button -
I am trying to create a quiz that registers the pulling and dropping actions, so if you have the right 'squares' Drag it to the right 'box' and then click the Submit button, the result is displayed.
The problem I have is: How would Javascript be understood which section is pressed after which box is pressed?
& lt ;! DOCTYPE HTML & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Style & gt; # Div1, # div2 {float: left; Width: 50px; Height: 50px; Margin: 3px;} # div3, # div4 {float: left; Width: 56px; Height: 56px; Margin: 3px;} & lt; / Style & gt; & Lt; Script & gt; Function allowDrop (ev) {ev.preventDefault ()}} drag the function (ev) {ev.dataTransfer.setData ("text", ev.target.id);} function drop (ev) {ev.preventDefault (); Var data = ev.dataTransfer.getData ("text"); Ev.target.appendChild (document.getElementById); } & Lt; / Script & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; Center & gt; & Lt; Div style = "position: full; left: 50px; top: 0px;" & Gt; & Lt; Div id = "div1" style = "background: # 00f" draggable = "true" ondragstart = "drag (event)" id = "drag1" & gt; & Lt; / Div & gt; & Lt; Div id = "div2" style = "background: # f00" draggable = "true" ondragstart = "drag (event)" id = "drag2" & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div style = "position: full; left: 40px; top: 100px;" & Gt; & Lt; Div id = "div3" style = "border: 3px solid # 00f" ondrop = "drop (event)" ondragover = "allowdrop (event)" & gt; & Lt; / Div & gt; & Lt; Div id = "div4" style = "border: 3px solid # f00" ondrop = "drop (event)" ondragover = "allowdrop (event)" & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div style = "position: full; left: 75px; top: 200px;" & Gt; & Lt; Button type = "button" class = "myBttn" onclick = "doSomething ()" & gt; Submit & lt; / Button & gt; & Lt; / Div & gt; & Lt; / Center & gt; & Lt; / Body & gt; & Lt; / Html & gt; What if you want to drag and drop events Use them to compare. Compare them.
& lt; Div style = "position: full; left: 50px; top: 0px;" & Gt; & Lt; Div id = "sourceB" class = "b" dragable = "true" ondragstart = "drag (event)" data-target-id = "tb" & gt; & Lt; / Div & gt; & Lt; Div id = "sourceRed" class = "r" dragable = "true" ondragstart = "drag (event)" data-target-id = "tr" & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div style = "position: full; left: 40px; top: 100px;" & Gt; & Lt; Div id = "tb" class = "tb" ondrop = "drop (event)" ondragover = "allowdrop (event)" data-match = "sourceb" & gt; & Lt; / Div & gt; & Lt; Div id = "tr" class = "tr" ondrop = "drop (event)" ondragover = "allowdrop (event)" data-match = "source r" & gt; & Lt; / Div & gt; & Lt; / Div & gt; This is Javascript
var droppedEmentId; Function drag (ev) {ev.dataTransfer.setData ("text", ev.target.id); Console.log ("ev.target.id =", ev.target.id); Left elementid = ev.target.id; } Function drop (ev) {ev.preventDefault (); Var target element = document. GetElementById (ev.srcElement.id); Var dropTarget = targetElement.getAttribute ("data-match"); Console.log ("ev.srcElement.id =", ev.srcElement.id); If (droppedElementId == dropTarget) {console.log ("dropTarget =" + dropTarget); Console.log ("droppedElementId =" + ElementI has been dropped); }; Var data = ev.dataTransfer.getData ("text"); Ev.target.appendChild (document.getElementById); } This is just a quick shot and the cross browser has not been tested. This - it's probably.
Comments
Post a Comment