Я пытаюсь добавить перетаскивание в соответствии с этим руководством
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&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.
.