gpt4 book ai didi

javascript - 当这个选择器还不存在时,它如何在 Jquery 中工作?

转载 作者:行者123 更新时间:2023-11-28 13:40:58 24 4
gpt4 key购买 nike

我在网上找到了这个教程,帮助我通过以下链接了解 Jquery 的拖放功能以及这个卡片映射小游戏... http://www.elated.com/res/File/articles/development/javascript/jquery/drag-and-drop-with-jquery-your-essential-guide/card-game.html

第37行,为什么是$('<div>' + numbers[i] + '</div>')在它甚至还不存在时选择,就我而言,当它们已经存在于文档中时,您在 Jquery 中选择它们。我不太明白那个选择器,有人可以详细说明一下发生了什么吗那个选择器?即使在第 48 行...也是 ui Jquery 中的内置单词 .. 当他们说 ui.draggable 时? ui 是什么意思具体指的是?

谢谢!

<!doctype html>
<html lang="en">
<head>

<title>A jQuery Drag-and-Drop Number Cards Game</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<link rel="stylesheet" type="text/css" href="style.css">

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.9/jquery-ui.min.js"></script>
<script type="text/javascript">

var correctCards = 0;
$( init );

function init() {

// Hide the success message
$('#successMessage').hide();
$('#successMessage').css( {
left: '580px',
top: '250px',
width: 0,
height: 0
} );

// Reset the game
correctCards = 0;
$('#cardPile').html( '' );
$('#cardSlots').html( '' );

// Create the pile of shuffled cards
var numbers = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ];
numbers.sort( function() { return Math.random() - .5 } );

for ( var i=0; i<10; i++ ) {
$('<div>' + numbers[i] + '</div>').data( 'number', numbers[i] ).attr( 'id', 'card'+numbers[i] ).appendTo( '#cardPile' ).draggable( {
containment: '#content',
stack: '#cardPile div',
cursor: 'move',
revert: true
} );
}

// Create the card slots
var words = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten' ];
for ( var i=1; i<=10; i++ ) {
$('<div>' + words[i-1] + '</div>').data( 'number', i ).appendTo( '#cardSlots' ).droppable( {
accept: '#cardPile div',
hoverClass: 'hovered',
drop: handleCardDrop
} );
}

}

function handleCardDrop( event, ui ) {
var slotNumber = $(this).data( 'number' );
var cardNumber = ui.draggable.data( 'number' );

// If the card was dropped to the correct slot,
// change the card colour, position it directly
// on top of the slot, and prevent it being dragged
// again

if ( slotNumber == cardNumber ) {
ui.draggable.addClass( 'correct' );
ui.draggable.draggable( 'disable' );
$(this).droppable( 'disable' );
ui.draggable.position( { of: $(this), my: 'left top', at: 'left top' } );
ui.draggable.draggable( 'option', 'revert', false );
correctCards++;
}

// If all the cards have been placed correctly then display a message
// and reset the cards for another go

if ( correctCards == 10 ) {
$('#successMessage').show();
$('#successMessage').animate( {
left: '380px',
top: '200px',
width: '400px',
height: '100px',
opacity: 1
} );
}

}

</script>

</head>
<body>

<div class="wideBox">
<h1>Drag-and-Drop with jQuery: Your Essential Guide</h1>
<h2>A Number Cards Game</h2>
</div>

<div id="content">

<div id="cardPile"> </div>
<div id="cardSlots"> </div>

<div id="successMessage">
<h2>You did it!</h2>
<button onclick="init()">Play Again</button>
</div>

</div>

<div class="wideBox">
<p>&copy; Elated.com | <a href="http://www.elated.com/articles/drag-and-drop-with-jquery-your-essential-guide/">Back to Tutorial</a></p>
<p style="font-size: .8em; line-height: 1.5em;"><a rel="license" href="http://creativecommons.org/licenses/by/3.0/"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by/3.0/88x31.png" /></a><br />This <span xmlns:dc="http://purl.org/dc/elements/1.1/" href="http://purl.org/dc/dcmitype/Text" rel="dc:type">work</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="http://www.elated.com/" property="cc:attributionName" rel="cc:attributionURL">http://www.elated.com/</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a>.</p>
</div>

</body>
</html>

最佳答案

表达式:

    $('<div>' + numbers[i] + '</div>')

不是选择器。当参数为jQuery时是一段 HTML(即以 < 开头),它创建与 HTML 相对应的新 DOM 元素,但不会在 DOM 中搜索它们。

$("div")是一个选择器,$("<div>")是一位创造者。

ui是一个变量名,它是 handleCardDrop 的第二个参数功能。由于此函数是 jQuery UI 使用的回调 Draggable widget,它接收实例作为参数,并且 ui.draggable然后可以用来引用小部件的功能。

关于javascript - 当这个选择器还不存在时,它如何在 Jquery 中工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17864352/

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