gpt4 book ai didi

javascript - jQuery 拖放 : Dragging an enclosing element

转载 作者:行者123 更新时间:2023-11-30 06:22:34 24 4
gpt4 key购买 nike

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

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或一些 hack,但不确定这是正确的。我试过了,但收到错误 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>

关于javascript - jQuery 拖放 : Dragging an enclosing element,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52306360/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com