gpt4 book ai didi

javascript - 如何带参数调用coffescript函数?

转载 作者:行者123 更新时间:2023-11-28 18:40:02 25 4
gpt4 key购买 nike

我创建了一个 coffescript 函数,其参数如下所示:

enlargeSelectionOn : (zoneSelected) ->
$(zoneSelected).stop().animate
width:450
height:510
1000

html 部分如下所示:

 #container1
.title1
img(src="img/team.png")
span.text First Title
a(href="#")

CSS 部分(带手写笔)看起来像

#container1
width 398px
height 490px
float left
margin-right 11px
border 2px solid $color6
background-color $color5
border-bottom-right-radius: 50px;
box-shadow 4px 4px 8px #aaa
.title1
height 53px
width 100%
border-bottom 1px solid $color6
color white
background-color $color6

我创建了一个函数,当我将鼠标悬停在 div 上时为它的尺寸设置动画所以我想用悬停事件来调用它:

    'hover div#container1' : 'enlargeSelectionOn("container1")'

但是这不起作用!我知道我的代码有问题

当我调用它时不带这样的参数

  'hover div#container' : 'enlargeSelectionOn'

工作正常

但在这种情况下,我被迫在函数内调用所选的 div

enlargeSelectionOn : () ->
$('div#container1').stop().animate
width:450
height:510
1000

我想使用参数,因为我要将这个函数用于不同的 div 容器。

最佳答案

我认为您编写函数的方式不是正确的语法。应该是:

enlargeSelectionOn = (zoneSelected) ->
$(zoneSelected).stop().animate
width:450
height:510
1000

要在悬停事件上调用此函数,您需要告诉 jQuery 在您的 div 悬停时调用函数 enlargeSelectionOn。为此,请使用以下代码:

$("div#container").hover(enlargeSelectionOn("container1"))

有关悬停功能的更多信息,请参阅 in the jQuery docs .

关于javascript - 如何带参数调用coffescript函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36284299/

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