编辑**
在我的文字游戏中,有一个包含3个字母单词的网格。
游戏的目的是通过点击侧面的相应字母来拼写单词。
当网格中的某个区域突出显示时,它会向用户显示要拼写的单词。用户单击网格侧面的字母,然后移动到突出显示的区域。
单击一个字母时,它将动画显示网格中的相应区域。我希望尽可能使用户友好,所以我想另外添加“keyDown()”,以便当用户按下“a”时,它会在屏幕上显示动画。
这些字母存储在HTML中
<div class="tiles-wrapper">
<div id="drag1" class="drag ui-widget-content player-tiles2" tabindex="0" data-letter="a">
<p>a</p>
</div>
<div id="drag2" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="b">
<p>b</p>
</div>
<div id="drag3" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="c">
<p>c</p>
</div>
<div id="drag4" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="d">
<p>d</p>
</div>
<div id="drag5" class="drag ui-widget-content player-tiles2" tabindex="0" data-letter="e">
<p>e</p>
</div>
<div id="drag6" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="f">
<p>f</p>
</div>
<div id="drag7" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="g">
<p>g</p>
</div>
<div id="drag8" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="h">
<p>h</p>
</div>
<div id="drag9" class="drag ui-widget-content player-tiles2" tabindex="0" data-letter="i">
<p>i</p>
</div>
<div id="drag10" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="j">
<p>j</p>
</div>
<div id="drag11" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="k">
<p>k</p>
</div>
<div id="drag12" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="l">
<p>l</p>
</div>
<div id="drag13" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="m">
<p>m</p>
</div>
<div id="drag14" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="n">
<p>n</p>
</div>
<div id="drag15" class="drag ui-widget-content player-tiles2" tabindex="0" data-letter="o">
<p>o</p>
</div>
<div id="drag16" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="p">
<p>p</p>
</div>
<div id="drag17" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="q">
<p>q</p>
</div>
<div id="drag18" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="r">
<p>r</p>
</div>
<div id="drag19" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="s">
<p>s</p>
</div>
<div id="drag20" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="t">
<p>t</p>
</div>
<div id="drag21" class="drag ui-widget-content player-tiles2" tabindex="0" data-letter="u">
<p>u</p>
</div>
<div id="drag22" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="v">
<p>v</p>
</div>
<div id="drag23" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="w">
<p>w</p>
</div>
<div id="drag24" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="x">
<p>x</p>
</div>
<div id="drag25" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="y">
<p>y</p>
</div>
<div id="drag26" class="drag ui-widget-content player-tiles" tabindex="0" data-letter="z">
<p>z</p>
</div>
我有一些脚本,但我不知道如何链接
$(document).keypress(function(e){
if (e.which >= 97 && e.which <= 122) {
animateDrag(String.fromCharCode(e.which));
}
});
答案 0 :(得分:1)
你可以通过在相应的字母div上触发click
事件来实现,如下所示:
$(document).keypress(function(e){
if (e.which >= 97 && e.which <= 122) {
letter = String.fromCharCode(e.which);
$(".drag[data-letter='"+letter+"']").trigger('click');
}
});
见工作demo
答案 1 :(得分:0)
$().ready(function() {
$('div.player-tiles, div.player-tiles2').keypress(function(e) {
if (e.which >= 97 && e.which <= 122) {
animateDrag(String.fromCharCode(e.which));
}
})
})