javascript - dropBefore 事件未按预期工作

标签 javascript events dom-events redips.drag

使用 REDIPS 出色的拖放库,我一直在尝试找出为什么 droppedBefore 事件不触发。

我已经删减了简单的示例,并将文件上传到此处以方便重现:http://tempsend.com/8B0DF444D3,这里是一个实时片段,可以快速向您展示该问题。

"use strict";

// define redips_init variable
var redips = {},		// redips container
	rd = REDIPS.drag,    // reference to the REDIPS.drag library
    msg;

// redips initialization
redips.init = function () {
	msg = document.getElementById('message');

	// how to display disabled elements
	rd.style.borderDisabled = 'solid';	// border style for disabled element will not be changed (default is dotted)
	rd.style.opacityDisabled = 60;		// disabled elements will have opacity effect
	// initialization
	rd.init();
	// only "smile" can be placed to the marked cell
	//rd.mark.exception.d8 = 'smile';
    // shift DIV elements with animation
	rd.shift.animation = true;
    // disabled elements will have opacity effect
	rd.style.opacityDisabled = 50;
    // set hover color
	rd.hover.colorTd = '#9BB3DA';
	// prepare handlers
	rd.event.cloned = function () {
	    // display message
	    var pos = rd.getPosition();
	    msg.innerHTML += ' FROM ' + pos[1] + ' ' + pos[2] + '<br>'
	};
	rd.event.droppedBefore = function (targetCell) {
	    alert('THIS EVENT FIRED YAY@!!!!');     //<-- SHOW THIS TO WIN THE PRIZE!
	};

	rd.event.deleted = function (cloned) {
		// if cloned element is directly moved to the trash
	    if (cloned) {
			var pos = rd.getPosition();
			msg.innerHTML += ' TO ' + pos[1] + ' ' + pos[2] + '<br>';
		}
		else {
			msg.innerHTML += 'Deleted';
		}
	};

};



// add onload event listener
if (window.addEventListener) {
    window.addEventListener('load', redips.init, false);
}
else if (window.attachEvent) {
    window.attachEvent('onload', redips.init);
}
/*
Darko Bunic
http://www.redips.net/
Jun, 2011.
*/

body {
	font-family: arial;
	margin: 0px; /* for IE6 / IE7 */
}

/* add bottom margin between tables */
#table1,
#table2 {
	margin-bottom: 20px;
}

/* drag container */
#redips-drag {
	margin: auto;
	width: 680px;
}

/* set border for images inside DRAG region - exclude image margin inheritance */
/* my WordPress theme had some funny margin settings */
#redips-drag img {
	margin: 1px;
}

/* drag objects (DIV inside table cells) */
.redips-drag {
	cursor: move;
	margin: auto;
	margin-bottom: 1px;
	margin-top: 1px;
	z-index: 10;
	background-color: white;
	text-align: center;
	font-size: 10pt; /* needed for cloned object */
	width: 140px;
	height: 150px;  /* objects height */
	line-height: 20px;
	/* round corners */
	border-radius: 4px; /* Opera, Chrome */
	-moz-border-radius: 4px; /* FF */
}


/* drag objects border for the first table */
.t1 {
	border: 2px solid #499B33;
}
/* drag object border for the second table */
.t2 {
	border: 2px solid SteelBlue;
}
/* cloned objects - third table */
.t3 {
	border: 2px solid #FF8A58;
}
/* allow / deny access to cells marked with 'mark' class name */
.redips-mark {
	color: white;
	background-color: #9B9EA2;
}
/* trash cell */
.redips-trash {
	color: white;
	background-color: #e0e0e0;
}

.icontext {
    color: black;
}

/* tables */
div#redips-drag table {
	background-color: #e0e0e0;
	border-collapse: collapse;
}


/* input elements in dragging container */
div#redips-drag input {
	cursor: auto;
}
	/* height for input text in DIV element */
	div#drag #d13 input {
		height: 13px;
	}
	/* height for dropdown menu in DIV element */
	div#drag #d5 select {
		height: 20px;
	}

/* table cells */
div#redips-drag td {
	height: 32px;
	border: 1px solid white;
	text-align: center;
	font-size: 10pt;
	padding: 2px;
}

/* "Click" button */
.button {
	margin-bottom: 2px;
	background-color: #6A93D4;
	color: white; 
	border-width: 1px;
	width: 44px;
	padding: 0px;
}


/* toggle checkboxes at the bottom */
.checkbox {
	margin-left: 13px;
	margin-right: 14px;
	width: 13px; /* needed for IE ?! */
}


/* message cell */
.message_line {
	padding-left: 10px;
	margin-bottom: 3px;
	font-size: 10pt;
	color: #888;
}
<!DOCTYPE html>
<html><head>
		<meta name="description" content="Description">
		<meta name="viewport" content="width=device-width, user-scalable=no"><!-- "position: fixed" fix for Android 2.2+ -->
		  <link rel="stylesheet" href="./dragDrop_files/12112015.css" type="text/css" media="screen">

		<script type="text/javascript" src="http://www.redips.net/my/preview/REDIPS_drag/redips-drag-min.js"></script>
		
 <script type="text/javascript" src="./dragDrop_files/script.js"></script>
		<title>Droppable Event</title>
	</head>
	<body>
		
	<div style="background-color:#eee;padding:10px;text-align:center;font-size:20px;font-weight:bold">A</div>
		<br>
		<div id="redips-drag">
		<!-- tables inside this DIV could have draggable content -->
			<div style="background-color:#eee;padding:10px;text-align:center;font-size:20px;font-weight:bold"><a href="">Drop Before Event Not Working</a></div>
        
			<table id="FSU4">
				<colgroup><col width="160"><col width="160"><col width="160"><col width="40"><col width="160"></colgroup>
				<tbody><tr height="150px" style="background-color: #eee">
					<td class="redips-trash" title="adsf"><div id="34563463" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">sdafsfd</a><BR></div></td>
					<td class="redips-trash" title="ertrt"><div id="2765657" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">fghdfghH</a></div></td>
					<td class="redips-trash" title="erter"><div id="3456347" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">ljkljhsdf</a></div></td>
					<td class="redips-mark"></td>
					<td class="redips-mark"></td>
				</tr>
				<tr height="150px">
					<td class="redips-mark"></td>
					<td class="redips-mark"></td>
					<td class="redips-mark"></td>
					<td class="redips-mark"></td>
					<td class="redips-mark"></td>
				</tr>
			</tbody></table>
			
			<div  id="message" > </div>

			<div><input type="button" value="Save1" class="button" onclick="save(&#39;plain&#39;)" title="Send content to the server (it will only show accepted parameters)"><span class="message_line">Save content of the first table (plain query string)</span></div>
			<div><input type="button" value="Save2" class="button" onclick="save(&#39;json&#39;)" title="Send content to the server (it will only show accepted parameters)"><span class="message_line">Save content of the first table (JSON format)</span></div>
			<div><input type="radio" name="drop_option" class="checkbox" onclick="setMode(this)" value="multiple" title="Enabled dropping to already taken table cells" checked="true"><span class="message_line">Enable dropping to already taken table cells</span></div>
			<div><input type="radio" name="drop_option" class="checkbox" onclick="setMode(this)" value="single" title="Disabled dropping to already taken table cells"><span class="message_line">Disable dropping to already taken table cells</span></div>
			<div><input type="radio" name="drop_option" class="checkbox" onclick="setMode(this)" value="switch" title="Switch content"><span class="message_line">Switch content</span></div>
			<div><input type="radio" name="drop_option" class="checkbox" onclick="setMode(this)" value="switching" title="Switching content continuously"><span class="message_line">Switching content continuously</span></div>
			<div><input type="radio" name="drop_option" class="checkbox" onclick="setMode(this)" value="overwrite" title="Overwrite content"><span class="message_line">Overwrite content</span></div>
			<div><input type="checkbox" class="checkbox" onclick="toggleDeleteCloned(this)" title="Remove cloned object if dragged outside of any table" checked="true"><span class="message_line">Remove cloned element if dragged outside of any table</span></div>
			<div><input type="checkbox" class="checkbox" onclick="toggleConfirm(this)" title="Confirm delete"><span class="message_line">Confirm delete</span></div>
			<div><input type="checkbox" class="checkbox" onclick="toggleDragging(this)" title="Enable dragging" checked="true"><span class="message_line">Enable dragging</span></div>
		<div id="redips_clone" style="height: 1px; width: 1px;"></div></div>
	
</body></html>

要查看问题,请运行代码片段并将一个框拖到另一个框上,应显示一条警告:THIS EVENT FIRED YAY@!!!!

我从 http://www.redips.net/my/preview/REDIPS_drag/example26/ 中获取了上述代码,它有一个有效的 rd.event.droppedBefore 事件。我已经将我的缩减重现与示例进行了 10 次比较,但仍然无法确定为什么该事件没有被触发?另外 2 个事件 rd.event.cloned 和 rd.event.deleted 都可以工作。

最佳答案

由于此代码,它不起作用,

原始代码:

<tr height="150px" style="background-color: #eee">
                    <td class="redips-trash" title="adsf"><div id="34563463" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">sdafsfd</a><BR></div></td>
                    <td class="redips-trash" title="ertrt"><div id="2765657" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">fghdfghH</a></div></td>
                    <td class="redips-trash" title="erter"><div id="3456347" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">ljkljhsdf</a></div></td>
                    <td class="redips-mark"></td>
                    <td class="redips-mark"></td>

固定代码:

 <tr height="150px" style="background-color: #eee">
                        <td  title="adsf"><div id="34563463" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">sdafsfd</a><BR></div></td>
                        <td title="ertrt"><div id="2765657" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">fghdfghH</a></div></td>
                        <td title="erter"><div id="3456347" class="redips-drag t3 redips-clone" style="border-style: solid; cursor: move;"><a href="http://abc.com.au" title="abc">ljkljhsdf</a></div></td>
                        <td class="redips-mark"></td>
                        <td class="redips-mark"></td>

原因很简单,要么你把 class="redips-drag Orange redips-clone"要么 class="redips-trash"都不给一个元素。你给了 td redips-trash ,里面给了 div redips-drag ,我认为这是一个困惑。根据示例链接,我发现了那个错误。如果您想确保它不会重复,请删除 redips-clone

关于javascript - dropBefore 事件未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33666301/

相关文章:

javascript - 将附加参数传递给事件回调函数

javascript - Node.JS : forEach vs for loop Asynchronous nature

javascript - 如何将 DOM 与附加事件一起保存?

javascript - 如何在 react.js 中使组件在滚动时显示/隐藏

java事件队列事件调度刷新/陷阱事件

javascript - 不可见元素不接收鼠标事件

javascript - RxJS Observable.fromEvent 链为每个订阅者触发

events - 如何在 dojo 中发起一个全局事件?

Javascript 对象到具有多个级别的 XML 字符串

javascript - React List渲染顺序必须一致?