HTML5 includes new attributes that can be used for Drag and Drop effects.
Here are the HTML5 attributes you can use to create drag and drop effects:
draggable
- This attribute specifies whether an element is draggable or not (values: true, false, or auto).ondrag
- This event fires continuously during a drag operation.ondragstart
- This event is fired when the user starts to drag the selection.ondragenter
- Fires on the target element when the user drags the object to a valid drop target.ondragover
- This event fires continuously while the user drags an object over a valid drop target.ondragleave
- This event occurs when the user drags an object on the page outside the area of a valid drop target.ondragend
- Occurs when the user stops dragging the element (releases the mouse) to which this attribute is applied.ondrop
- Occurs on a possible target element when the dragged data is dropped on it; comes immediately after the ondragend
event.draggable
attribute to 'true', and add also the ondragstart
attribute.
ondrop
attribute, and cancel the ondragover
event (using the value: 'return false
', or a function with event.preventDefault()
). To be more sure, cancel also the ondragenter
event.
<!doctype html> <html> <head> <title>title</title> <style> #drag1 { width: 12em; margin: .2em; background-color: #f0f0b0; border: 2px dashed #000; padding: .2em .5em; color: #0001da; cursor: pointer; } #target_drop1 { width: 20em; min-height: 5em; margin: 1em; background-color: #ededfe; padding: .2em .5em; border: 2px solid #000; } </style> </head> <body> <h4>Example Drag and Drop HTML5 - JavaScript</h4> <div draggable='true' ondragstart='dragStart(event)' id='drag1'>Drag this.<br/>http://www.coursesweb.net/</div> <div id='target_drop1' ondrop='drop(event)' ondragenter='return false;' ondragover='allowDrop(event)'>Drop here</div> <script> function allowDrop(ev) { ev.preventDefault(); // cancel the ev event } // function called when the drag operation starts function dragStart(ev) { // sets the data type and the value of the dragged data // This data will be returned by getData(). Here the ID of current dragged element ev.dataTransfer.setData('Text', ev.target.id); } // function called when the dragged element is dropped function drop(ev) { ev.preventDefault(); // the dragover event needs to be canceled to allow firing the drop event // gets data set by setData() for current drag-and-drop operation (the ID of current dragged element), // using for parameter a string with the same format set in setData var drag_data = ev.dataTransfer.getData('Text'); // adds /appends the dropped element in the specified target ev.target.appendChild(document.getElementById(drag_data)); } </script> </body> </html>
function drop(ev) { ev.preventDefault(); var drag_data = ev.dataTransfer.getData('Text'); ev.target.appendChild(document.getElementById(drag_data)); }- preventDefault() to prevent the browser default handling of the data (default is open as link in browser on drop).
<!doctype html> <html> <head> <title>title</title> <style> #div1, #div2 { float: left; width: 12em; min-height: 75px; margin: .2em; border: 2px solid #000; padding: .2em; } #div2 { background:#bbfbbb; } .todrag { cursor: pointer; } .dragging { border: 1px dashed #00f; background: #ff1; } </style> </head> <body> <h4>Example Drag and Drop image - HTML5 - JavaScript</h4> <p>Click, drag and drop the image from one rectangle to another and back.</p> <div id='div1' ondrop='drop(event)' ondragover='allowDrop(event)'> <img src='../imgs/webcourses.gif' draggable='true' ondragstart='dragStart(event)' id='drag2' class='todrag' width='191' height='63' alt='Drag-and-Drop image' /> </div> <div id='div2' ondrop='drop(event)' ondragover='allowDrop(event)'></div> <br style='clear:both' /> <script> function allowDrop(ev) { ev.preventDefault(); // cancel the ev event } // function called when the drag operation starts function dragStart(ev) { // sets the data type and the value of the dragged data // This data will be returned by getData(). Here the ID of current dragged element ev.dataTransfer.setData('Text', ev.target.id); // sets another css class ev.target.className = 'dragging'; } // function called when the dragged element is dropped function drop(ev) { ev.preventDefault(); // the dragover event needs to be canceled to allow firing the drop event // gets data set by setData() for current drag-and-drop operation (the ID of current dragged element), // using for parameter a string with the same format set in setData var drag_data = ev.dataTransfer.getData('Text'); // adds /appends the dropped element in the drop-target ev.target.appendChild(document.getElementById(drag_data)); // sets another css class document.getElementById(drag_data).className = 'todrag'; } </script> </body> </html>
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net