我试图用相同的热键打开和关闭模态窗口:
if !$('#new-stuff').length or $('#new-stuff').is(':hidden')
$(document).keydown( (e) ->
if e.altKey && e.which == 65
e.preventDefault
modal.open(
content: '<input type="text" placeholder="New stuff here!" id="new-stuff"><br /><input type="submit" value="Do stuff!">'
)
).keyup( ->
$('#new-stuff').focus()
)
else
$(document).keyup( (e) ->
if e.altKey && e.which == 65
e.preventDefault()
modal.close()
)
当我按下alt + A
模式是开放的,重点是输入。但是当我再次按alt + A
时,它会再次关注输入,而不是关闭。
!$('#new-stuff').length or $('#new-stuff').is(':hidden')
两者都是false
,但$('#new-stuff').focus()
正在发生。为什么?
和如何用alt + A
关闭模态
更新
以下是工作代码:
$(document).keydown( (e) ->
if e.altKey && e.which == 65
if $('#new-stuff').is(':visible')
e.preventDefault()
modal.close()
else
e.preventDefault
modal.open(
content: '<input type="text" placeholder="New stuff here!" id="new-stuff"><br /><input type="submit" value="Do stuff!">'
)
).keyup( ->
$('#new-stuff').focus() if $('#new-stuff').is(':visible')
)
发布于 2014-01-29 00:21:04
看起来,您需要将条件放在事件函数中。就像这样:
$(document).keyup( (e) ->
if e.altKey && e.which == 65
if $('#new-stuff:visible)')
# hide it
else
# show it
)
https://stackoverflow.com/questions/21419520
复制相似问题