jQuery 拖放:拖动封闭元素



>我正在尝试根据本指南添加拖放

https://www.w3schools.com/HTML/html5_draganddrop.asp

我有

CoffeeScript
$('#topic-list').on 'dragstart', '.topic-draggable', (event) ->
console.log event
event.originalEvent.dataTransfer.setData("text", event.target.id)
console.log event.target
console.log event.target.id
HTML (ERB)
<li draggable="true" class="topic-draggable" id="topic_{{{topic_id}}}">
<a href='<%= topics_path %>/{{{topic_id}}}'>

这些项目是动态插入的,*_id被数字替换。问题是当我尝试拖动<li>时,event.target<a>元素,target.id是空白的。

Browser console
<a href="/topics/2">
<i class="fa fa-th"></i>
<span class="topic-name">Life Span Development</span>
<i class="fa fa-angle-right"></i>
</a>

如何拖放<li>元素,而不是<a>元素?

我可以使用event.target.parent或一些技巧,但不确定是否正确。我试过了,但错误Uncaught TypeError: Cannot read property 'parent' of undefined.

$('#topic-list').on('dragstart', '.topic-draggable', function(event) {
console.log(event);
event.originalEvent.dataTransfer.setData("text", event.target.id);
console.log(event.target);
return console.log(event.target.id);
});
ul#topic-list {
box-sizing: border-box;
color: white;
background-color: #20A0D0;
list-style: none;
padding: 0;
margin: 0;
}
li {
list-style: none;
}
li:nth-child(even) {
background-color: #3DB1E7;
}
a {
padding: 20px 20px;
display: flex;
font-size: 1.4em;
text-decoration: none;
color: white;
justify-content: space-between;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<ul id="topic-list" style="display: block;">
<li draggable="true" class="topic-draggable" id="topic_1">
<a href="/topics/1">
<i draggable="true" class="fa fa-th topic-draggable"></i>
<span class="topic-name">Introduction to A&amp;P</span>
<i class="fa fa-angle-right"></i>
</a>
</li>
<li draggable="true" class="topic-draggable" id="topic_2">
<a href="/topics/2">
<i draggable="true" class="fa fa-th topic-draggable"></i>
<span class="topic-name">Life Span Development</span>
<i class="fa fa-angle-right"></i>
</a>
</li>
<li draggable="true" class="topic-draggable" id="topic_3">
<a href="/topics/3">
<i draggable="true" class="fa fa-th topic-draggable"></i>
<span class="topic-name">Medical Terminology</span>
<i class="fa fa-angle-right"></i>
</a>
</li>
</ul>

我实际上无法创建示例,因为 StackOverflow 的代码编辑器给出了错误DOMException: Blocked a frame with origin "null" from accessing a cross-origin frame..

好吧,在惊愕之后,我终于得到了它。

CoffeeScript
topicDrag = false
currentTarget = null
$('#topic-list').on 'dragstart', '.topic-draggable', (event) ->
li = $(event.target).parent().parent()
event.originalEvent.dataTransfer.setData("text", li.attr('id'))
li.css('border', '1px dashed red').css('opacity', '0.3')
topicDrag = true
$('#topic-list').on 'dragover', 'li', (event) ->
event.preventDefault() if topicDrag # allow drop, but only for our topics, not random links
$('#topic-list').on 'dragenter', 'li', (event) ->
return unless topicDrag
# browser will call dragleave after dragenter when moving from one <li> to another, turning off the border. fix order.
currentTarget = event.target # save only the very last target
setTimeout ->
# console.log 'dragenter '+currentTarget.nodeName
$(currentTarget).parentsUntil('#topic-list', 'li').css('border', '2px dashed yellow')
, 100
$('#topic-list').on 'dragleave', 'li', (event) ->
# console.log 'dragleave '+event.target.nodeName
# when jumping from li > a > span to another, browser calls events in wrong order. clear all.
$('#topic-list').children().css('border', 'initial')
$('#topic-list').on 'drop', 'li', (event) ->
event.preventDefault()
data = event.originalEvent.dataTransfer.getData("text")
# console.log data
source = $('#'+data)
# the browser allows dropping on child elements of <li> for some reason
$(event.target).parentsUntil('#topic-list', 'li').before( source ).css('border', 'initial')
source.css('opacity', 'initial').css('border', 'initial')
topicDrag = false
HTML-ERB
<li id="topic_{{{topic_id}}}">
<a href='<%= topics_path %>/{{{topic_id}}}'>
<%= fa_icon 'th', draggable: true, class: 'topic-draggable' %>
<span class='topic-name'>{{{topic_name}}}</span>
<%= fa_icon "angle-right" %>
</a>
</li>

最新更新