gpt4 book ai didi

javascript - 单击时未调用函数内的 Coffeescript 函数

转载 作者:行者123 更新时间:2023-12-03 10:21:59 27 4
gpt4 key购买 nike

我确信这很简单,但我无法在单击事件上调用此函数。我尝试过各种化身,但无法让它很好地发挥作用。如果我在每个点击事件中移动 if/else 循环,它就可以正常工作。我只是想通过将循环移动到单个引用函数来使代码稍微干燥一些。这是语法问题还是逻辑问题? - 提前致谢。

不起作用

$(".filter_option").click ->
$('#submission_list').children(".item ").show()
checkIfItemsEmpty()

$('#current_filters').on 'click', 'a.filter_remove', ->
$('#submission_list').children(".item").hide()
checkIfItemsEmpty()

checkIfItemsEmpty = () ->
if !$('.item').filter(':visible').length
$('#no_results').show()
console.log('The show command was triggered')
else
$('#no_results').hide()
console.log('The hide command was triggered')

工作

$(".filter_option").click ->
$('#submission_list').children(".item ").show()
if !$('.item').filter(':visible').length
$('#no_results').show()
console.log('The show command was triggered')
else
$('#no_results').hide()
console.log('The hide command was triggered')

$('#current_filters').on 'click', 'a.filter_remove', ->
$('#submission_list').children(".item").hide()
if !$('.item').filter(':visible').length
$('#no_results').show()
console.log('The show command was triggered')
else
$('#no_results').hide()
console.log('The hide command was triggered')

最佳答案

事实证明,这是一个简单的格式化案例,我在发布后两秒就意识到了!

似乎

checkIfItemsEmpty = () ->   
if !$('.item').filter(':visible').length
$('#no_results').show()
console.log('The show command was triggered')
else
$('#no_results').hide()
console.log('The hide command was triggered')

必须缩进超过 jQuery -> 才能工作。

所以....

jQuery ->
$(".filter_option").click ->
$('#submission_list').children(".item ").show()
checkIfItemsEmpty()

$('#current_filters').on 'click', 'a.filter_remove', ->
$('#submission_list').children(".item").hide()
checkIfItemsEmpty()

checkIfItemsEmpty = () ->
if !$('.item').filter(':visible').length
$('#no_results').show()
console.log('The show command was triggered')
else
$('#no_results').hide()
console.log('The hide command was triggered')

现在就像做梦一样。

关于javascript - 单击时未调用函数内的 Coffeescript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29585069/

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