首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >coffeescript: jquery单击函数只触发一次

coffeescript: jquery单击函数只触发一次
EN

Stack Overflow用户
提问于 2014-02-10 19:55:34
回答 1查看 125关注 0票数 0

我一直在寻找答案,却找不到有效的答案。

基本上,我有一个函数可以发布到facebook墙或twitter提要,但点击功能只触发一次。

下面是代码:

代码语言:javascript
复制
$('.social a').on 'click', (e) ->
    e.preventDefault()
    book = book.collectMeta $(@).parents('.book')

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

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

被触发的collectMeta函数如下:

代码语言:javascript
复制
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

第一次点击后收到的错误是: book.collectMeta不是一个函数

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-02-10 20:01:38

您的第一个单击是调用一个不存在的函数:book.collectMeta。这会扼杀你的脚本,所以在那之后的任何事情都是行不通的。

在我看来,这种情况可能会发生,因为您正在重写以下行中的book变量:

代码语言:javascript
复制
 book = book.collectMeta $(@).parents('.book')

因此,第一次单击将起作用,但随后的单击将不再起作用,因为图书不再具有与其相关的功能。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21686671

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档