【问题标题】:.each function running correctly once but then says 'undefined is not a function'.每个函数正确运行一次,然后说“未定义不是函数”
【发布时间】:2014-07-30 15:56:35
【问题描述】:

我正在编写一个简单的函数,它获取帖子的 URL,然后返回它在 Twitter 上被分享的次数。一个页面上有多个帖子,所以我需要为每个帖子运行该功能。下面是相关函数(注意我用的是RequireJS)。

function twitterCount($target) {

            $article = $target.closest($('article')),

            articleHREF = $target.parent().parent().attr('data-href');
            APIURL = 'https://cdn.api.twitter.com/1/urls/count.json';

            //Get count and set it as the inner HTML of .count
            $.getJSON(APIURL + "?callback=?&url=" + articleHREF, function(data) {
                $target.find('.count').html(data.count);
            });
        }

getTwitterCount: function() {
            $twitterLink.each(function() {
                $target = $(this);
                $target.function(twitterCount($target));
            });
        }

该函数正确运行一次,并给我页面上第一个帖子被喜欢的次数,但随后停止工作并吐出 Uncaught TypeError: undefined is not a function in控制台。我正在努力了解为什么它会运行一次但不适用于其他所有帖子。错误指向以下行...

$target.function(twitterCount($target));

应该输出的HTML在下面

<ul class="share-options" data-href="http://localhost/test-post/">
                <li><a class="twitter" href="javascript:void(0)">
                    Twitter <span class="count twitter-count"></span>
                </a></li>
                <li><a class="linkedin" href="javascript:void(0)">LinkedIn</a></li>
            </ul>

【问题讨论】:

  • 请从控制台添加 html 和错误。应该得到一个行号,可以帮助您了解问题所在
  • 有问题的行应该做什么?只是想将$target 传递给twitterCount() 吗?你在哪里定义$twitterLink
  • 脚本比较长,所以我不想全部粘贴进去。$twitterLink 只定义了$('.twitter')。有问题的行应该将当前正在迭代的项目传递给 twitterCount 函数。
  • 这里有点迷糊,$target.function(twitterCount($target));要做什么?

标签: javascript jquery loops each


【解决方案1】:

通过稍微修改您的代码,我取得了成功。

主要变化:

  1. 我将 .twitter 类引用更改为 .twitter-count 以匹配您的 HTML
  2. 我删除了您的 getTwitterLink 函数,只是直接执行了 each()
  3. 我将$target.function(twitterCount($target)) 替换为twitterCount($target) 这是一个很大的语法错误。

以下代码 cmets 的其他更改。

function twitterCount($target) {

    /* I simplified "parent().parent()" to "closest()" */
    articleHREF = $target.closest('ul').attr('data-href');
    APIURL = 'https://cdn.api.twitter.com/1/urls/count.json';

    $.getJSON(APIURL + "?callback=?&url=" + articleHREF, function (data) {
        /* I changed ".twitter" to ".twitter-count" to match the class in your HTML */
        $target.find('.twitter-count').html(data.count);
    }).fail( function(d, textStatus, error) {
        /* I added this fail handler, just in case */
        console.error("getJSON failed, status: " + textStatus + ", error: "+error)
    });
}

$twitterLink = $('a.twitter');

/* I removed the function here as it didn't seem necessary */
$twitterLink.each(function () {
    $target = $(this);
    /* Your function call here threw a syntax error.
       Replaced `$target.function(twitterCount($target))` with `twitterCount($target)` */
    twitterCount($target);
});

Working Example

【讨论】:

  • 非常感谢showdev,这已经解决了。就像你说的那样,我在这一行的语法很糟糕:$target.function(twitterCount($target))。 .each 之前的函数用作 RequireJS 的一部分以在特定时间运行函数,所以我保留了它。
猜你喜欢
  • 2015-07-01
  • 2011-12-27
  • 1970-01-01
  • 2019-06-27
  • 1970-01-01
  • 2015-03-16
  • 2015-03-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多