我正在尝试使用html,css,jquery和最终的javascript来制作问答游戏。目前,我想进行if声明,以检查是否在正确的div(城市)中删除了放置的元素。我有一个主意,可以通过类进行检查,并检查它们是否相同。这件事是否可能且适当?
我的问题是-如何比较两次潜水的课程?
$(function() {
$("#answers div").draggable();
$("#box div").droppable({
drop: function(event, ui) {
$(this)
.addClass("ui-state-highlight")
.find("p")
.html("Dropped!");
}
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUIZ</title>
<link href='style.css' rel='stylesheet'>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<h1>QUIZ</h1>
<div id="answers">
<div class="barcelona">
<p> Antoni Gaudi </p>
</div>
<div class="paris">
<p> Champ Elysees </p>
</div>
<div class="london">
<p> Tate Modern </p>
</div>
<div class="barcelona">
<p> Sagrada Familia </p>
</div>
<div class="paris">
<p> Montmartre </p>
</div>
<div class="ny">
<p> Fifth Avenue </p>
</div>
<div class="barcelona">
<p> Paella </p>
</div>
<div class="barcelona">
<p> La Rambla </p>
</div>
<div class="london">
<p> Piccadilly Circus </p>
</div>
<div class="paris">
<p> Mona Lisa </p>
</div>
<div class="ny">
<p> Empire State Building </p>
</div>
<div class="ny">
<p> Broadway </p>
</div>
<div class="paris">
<p> Musée d'Orsay </p>
</div>
<div class="ny">
<p> Wall Street </p>
</div>
<div class="london">
<p> Camden Town </p>
</div>
<div class="ny">
<p> Big Apple </p>
</div>
<div class="barcelona">
<p> La Boqueria </p>
</div>
</div>
<div id="box">
<div class="paris">
<p> PARIS </p>
</div>
<div class="ny">
<p> NY </p>
</div>
<div class="london">
<p> LONDON </p>
</div>
<div class="barcelona">
<p> BARCELONA </p>
</div>
</body>
</html>
摘自droppable的jQuery UI API文档:
drop(event,ui)
- 事件
- ui
- 可拖动:表示可拖动元素的jQuery对象。
- helper:表示要拖动的助手的jQuery对象。
- 位置:可拖动助手的当前CSS位置为{top,left}对象。
- 偏移量:可拖动助手的当前偏移位置为{顶部,左侧}对象。
您已经知道this
是可放置的,现在您也可以访问draggable
。 jQuery称为helper
的是光标之后的元素。默认情况下,它们是同一回事,但是您可以将可拖动的对象留在原处,直到您将其放下,直到一个幽灵般的克隆跟随您的光标为止。
虽然您可以给可拖动类一个类,然后在放置事件中使用类似这样的内容对其进行检查...
const categories = ["paris", "ny", "london", "barcelona"];
$(function() {
$("#answers div").draggable();
$("#box div").droppable({
drop: function(event, ui) {
const $this = $(this);
if (categories.some(c => $this.hasClass(c) && ui.draggable.hasClass(c))) {
$this
.addClass("ui-state-highlight")
.find("p")
.html("Valid :)");
} else {
$this
.addClass("ui-state-highlight")
.find("p")
.html("Invalid :(");
}
}
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>QUIZ</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<h1>QUIZ</h1>
<div id="answers">
<div class="barcelona">
<p> Antoni Gaudi </p>
</div>
<div class="paris">
<p> Champ Elysees </p>
</div>
<div class="london">
<p> Tate Modern </p>
</div>
<div class="ny">
<p> Fifth Avenue </p>
</div>
</div>
<div id="box">
<div class="paris">
<p> PARIS </p>
</div>
<div class="ny">
<p> NY </p>
</div>
<div class="london">
<p> LONDON </p>
</div>
<div class="barcelona">
<p> BARCELONA </p>
</div>
</div>
</body>
</html>
但是,我建议您进行长期思考。最终,我想您希望将难题存储在JavaScript中。
[
{ category: "Paris", answers: ["Champ Elysees", "Montmartre"] },
{ category: "NY", answers: ["Big Apple", "Broadway"] }
]
然后,您可以自动为拼图构建HTML,并将draggable
和droppable
小部件存储在每个类别中。像这样的东西:
categories.forEach(c => {
c.droppable = createDroppable(c.category);
c.draggables = c.answers.map(answer => createDraggable(answer));
});
然后,您可以根据身份比较可拖动对象和可拖放对象,而无需依赖属性。我对jQuery并不是很熟悉,但是我相信可以使用is
方法来完成。
// in the drop event
const category = categories.find(c => $(this).is(c.droppable));
if (category.draggables.some(answer => ui.draggable.is(answer)) {
// this answer is valid!
}
您可以使用ui.draggable
查找该类,并检查该类是否包含在当前元素中:
$( function() {
$( "#answers div" ).draggable();
$( "#box div" ).droppable({
drop: function( event, ui ) {
var dropped = $(this).attr('class').split(' ')[0];
if(ui.draggable.attr('class').split(' ').includes(dropped)){
$( this )
.addClass("ui-state-highlight")
.find("p")
.html("Dropped!");
}
}
});
});
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<h1>QUIZ</h1>
<div id = "answers">
<div class = "barcelona"><p> Antoni Gaudi </p></div>
<div class = "paris"><p> Champ Elysees </p></div>
<div class = "london"><p> Tate Modern </p></div>
<div class = "barcelona"><p> Sagrada Familia </p></div>
<div class = "paris"><p> Montmartre </p></div>
<div class = "ny"><p> Fifth Avenue </p></div>
<div class = "barcelona"><p> Paella </p></div>
<div class = "barcelona"><p> La Rambla </p></div>
<div class = "london"><p> Piccadilly Circus </p></div>
<div class = "paris"><p> Mona Lisa </p></div>
<div class = "ny"><p> Empire State Building </p></div>
<div class = "ny"><p> Broadway </p></div>
<div class = "paris"><p> Musée d'Orsay </p></div>
<div class = "ny"><p> Wall Street </p></div>
<div class = "london"><p> Camden Town </p></div>
<div class = "ny"><p> Big Apple </p></div>
<div class = "barcelona"><p> La Boqueria </p></div>
</div>
<div id = "box">
<div class = "paris"><p> PARIS </p></div>
<div class = "ny"><p> NY </p></div>
<div class = "london"><p> LONDON </p></div>
<div class = "barcelona"><p> BARCELONA </p></div>
</div>
尽管最好使用注释中建议的自定义属性:
$( function() {
$( "#answers div" ).draggable();
$( "#box div" ).droppable({
drop: function( event, ui ) {
var dropped = $(this).data('city');
if(ui.draggable.data('city') == dropped){
$( this )
.addClass("ui-state-highlight")
.find("p")
.html("Dropped!");
}
}
});
});
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<h1>QUIZ</h1>
<div id = "answers">
<div data-city="barcelona"><p> Antoni Gaudi </p></div>
<div data-city="paris"><p> Champ Elysees </p></div>
<div data-city="london"><p> Tate Modern </p></div>
<div data-city="barcelona"><p> Sagrada Familia </p></div>
<div data-city="paris"><p> Montmartre </p></div>
<div data-city="ny"><p> Fifth Avenue </p></div>
<div data-city="barcelona"><p> Paella </p></div>
<div data-city="barcelona"><p> La Rambla </p></div>
<div data-city="london"><p> Piccadilly Circus </p></div>
<div data-city="paris"><p> Mona Lisa </p></div>
<div data-city="ny"><p> Empire State Building </p></div>
<div data-city="ny"><p> Broadway </p></div>
<div data-city="paris"><p> Musée d'Orsay </p></div>
<div data-city="ny"><p> Wall Street </p></div>
<div data-city="london"><p> Camden Town </p></div>
<div data-city="ny"><p> Big Apple </p></div>
<div data-city="barcelona"><p> La Boqueria </p></div>
</div>
<div id="box">
<div data-city="paris"><p> PARIS </p></div>
<div data-city="ny"><p> NY </p></div>
<div data-city="london"><p> LONDON </p></div>
<div data-city="barcelona"><p> BARCELONA </p></div>
</div>