如何在拖放JavaScript中控制源TD样式?
在拖放循环中,您可以控制“拖动元素”和ondrop的所有样式,但也可以控制源的样式吗 在我的例子中,我将ancher元素从一个TD拖动到另一个TD。在函数drop(event)中,我可以根据自己的喜好设置目标TD的样式。请参阅下面的代码如何在拖放JavaScript中控制源TD样式?,javascript,php,html,css,drag-and-drop,Javascript,Php,Html,Css,Drag And Drop,在拖放循环中,您可以控制“拖动元素”和ondrop的所有样式,但也可以控制源的样式吗 在我的例子中,我将ancher元素从一个TD拖动到另一个TD。在函数drop(event)中,我可以根据自己的喜好设置目标TD的样式。请参阅下面的代码 // The actual drop event function drop(event) { // Prevent the default action for a drop (activate link) event.preventDefau
// The actual drop event
function drop(event) {
// Prevent the default action for a drop (activate link)
event.preventDefault();
// Get the ID from the dragged item
var iRecordID = event.dataTransfer.getData("text");
// Get the ID from the dropzone
var sXeduledata = event.target.id;
// Put the dragged element in the dropzone
event.target.appendChild(document.getElementById(iRecordID));
// Change the dropzone styling
event.target.style.backgroundColor = "lightgreen";
event.target.style.border = "";
event.target.style.textAlign = "center";
// Connect to a php file and saving the new data
$.post('dnd-update.php',
{sXeduleData:sXeduledata,iRecordNumber:iRecordID});
}
但我需要设计和准备“源”TD,以便作为“dropzone”重复使用。为了更精确,绿色光源需要变成白色目标。
我如何做到这一点
PHP源代码:
protected function showXeduleItemContent($p_aXeduleItem){
echo("<td class='text-center h5 back_green' class='droptarget' ondrop='drop(event)' ondragover='allowDrop(event)' id='".$sDay.":".$iRE.":".$aClassroomRecord[0]."'>");
echo("<a href='foo.php' draggable='true' id='".$p_aXeduleItem[0]."'>".$aLessonName[0][2]."</a></td>");
}
受保护函数showXeduleItemContent($p_aXeduleItem){
回声(“”);
回声(“”);
}
多亏了米尔肯32。他使我走上了正确的道路。
在对“parentNode”进行了深入研究之后,我找到了我需要的答案
答案是将“event.target.parentNode.style”添加到“dragstart”事件中
document.addEventListener("dragstart", function(event) {
// The event.dataTransfer.setData() method saves the data type for later use in the script
event.dataTransfer.setData("text", event.target.id);
// Styling of the source better know as parent
event.target.parentNode.style.backgroundColor = "white";
});
这与PHP无关。以后只包含HTML输出,不要标记为PHP。可能的重复也不应该使用HTML属性添加事件侦听器;现在是1998年。您已经安装了jQuery库,还可以使用它来设置事件侦听器和更新样式。