简体   繁体   English

coffeescript:jQuery单击功能仅触发一次

[英]coffeescript: jquery click function only fires once

I've searched for an answer and can't find one that works. 我搜索了一个答案,但找不到有效的答案。

Basically I have a function that posts to a facebook wall or a twitter feed, but the click function only fires once. 基本上,我有一个发布到Facebook墙或Twitter提要的函数,但是click函数仅触发一次。

Here's the code: 这是代码:

$('.social a').on 'click', (e) ->
    e.preventDefault()
    book = book.collectMeta $(@).parents('.book')

    if $(@).hasClass 'fb-share'
        postFacebook book

    if $(@).hasClass 'twitter-share'
        postTwitter book

My collectMeta function that gets fired is this: 我被触发的collectMeta函数是这样的:

book = 
    collectMeta: (dom) ->
        # Helper function that collects data of a book and prepares it 
        # for sharing on sociall media

        # The argument for this function accepts the parent DOM Object
        # that searches for the appropriate Meta Data

        $title = dom.find '.book-title'
        title = helper.cleanWhite $title.text()

        $author = dom.find '.book-author'
        author = helper.cleanWhite $author.text()

        $description = dom.find '.book-description'
        description = helper.cleanWhite $description.text()
        description = helper.trimText(description, 140)

        $link = dom.find '.book-link'
        link = glob.baseUrl() + $link.attr 'href'

        $image = dom.find '.book-image'
        image = glob.baseUrl() + $image.attr 'src'

        return book = 
            title: title
            caption: author
            description: description
            link: link
            image: image

And the error I receive after the first click, which works, is: book.collectMeta is not a function 第一次单击后收到的错误消息起作用了:book.collectMeta不是一个函数

Any help is greatly appreciated! 任何帮助是极大的赞赏!

Your first click is calling a function that doesn't exist: book.collectMeta . 您的第一次点击是调用一个不存在的函数: book.collectMeta This is killing your script, so nothing after that will work. 这正在杀死您的脚本,因此此后没有任何作用。

It looks to me like this might be happening because you are overriding the book variable in this line: 在我看来,这可能正在发生,因为您在这一行中覆盖了book变量:

 book = book.collectMeta $(@).parents('.book')

So the first click will work, but later ones will not because book no longer has a function associated with it. 因此,第一次单击将起作用,但以后的单击将不起作用,因为书不再具有与之关联的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM