使用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>





要查看问题,请运行代码段并将一个框拖到另一个框上,应显示一条警报,提示:此事件是YAY @ !!!!

我从http://www.redips.net/my/preview/REDIPS_drag/example26/中获取了上面的代码,该代码具有有效的rd.event.droppedBefore事件。我已将缩减后的repro与示例进行了十次比较,但仍然无法确定为什么未触发该事件?其他两个事件rd.event.clonedrd.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橙色redips-clone”放置,要么将class =“ redips-trash”都不同时赋予一个元素。你给了td redips-trash,里面给了div redips-drag,我想这就是一个困惑。根据示例链接,我发现了越野车。如果要确保不会重复,请删除redips-clone

07-28 09:40