JQuery .on("点击" ...间歇性地工作

时间:2015-02-26 18:34:40

标签: javascript jquery

我有一段代码:

$("body").on("click", ".reply-button", function(){
    alert("test");
});

当我点击动态生成的元素时(当执行此代码时它不是DOM的一部分),我想提醒我。

有时它的工作方式正如它所设想的那样。我点击按钮弹出一个小警报。但是,有时它会停止工作。我绑定它没有任何作用。如果我绑定到容器div(也是动态生成的),它确实有效,但是如果我将处理程序更改为合并按钮则不行。

我在问这个错误的可能原因是什么?我不知道怎么去调试这个。我的第一个猜测是,这是由于stopImmediatePropagationstopPropagation引起的,但我无法找到在同一区域使用的内容。

有没有人知道如何调试这个?

编辑: 如何生成DOM?

我从隐藏的模板中获取HTML。

var html = $("#template").html();

然后我将模板附加到div容器

$("#container").append(html);

EDIT2: 这是被拉的模板:

<div id="tweets-container" class="feed-wrapper">
</div>

<div id="tweet-template" style="display:none;">
    <!-- Tweet 1 -->
    <div class="tweet animated" data-scannedTweetId="s_id">
        <!-- User -->
        <div class="tweet-user">
            <!-- User picture -->
            <img class="tweet-user-picture" src="s_twt_owner_profile_img_url" />

            <!-- User info -->
            <div class="tweet-user-info">
                <!-- User name -->
                <div class="tweet-user-info-name">
                    <a href="//twitter.com/s_twt_owner_sn" target="_blank">s_twt_owner_name (@s_twt_owner_sn)</a>
                </div>

                <!-- User biography -->
                <div class="tweet-user-info-biography">
                    s_twt_owner_desc
                </div>
            </div>
        </div>

        <!-- User statistics (following, followers, and tweets) -->
        <span class="tweet-statistics animated">
            <div class="following">
                <div class="statistic-count"><a href="http://twitter.com/s_twt_owner_sn/following">s_twt_owner_num_following</a></div>
                <div class="statistic-label">following</div>
            </div>

            <div class="followers">
                <div class="statistic-count"><a href="http://twitter.com/s_twt_owner_sn/followers">s_twt_owner_num_follower</a></div>
                <div class="statistic-label">followers</div>
            </div>

            <div class="tweets">
                <div class="statistic-count"><a href="http://twitter.com/s_twt_owner_sn">s_twt_owner_num_twt</a></div>
                <div class="statistic-label">tweets</div>
            </div>
        </span>

        <!-- Tweet bars/graph -->
        <div class="side-information animated">
            <div class="bar-wrapper">
                <!-- Actual bars -->
                <div class="bars">
                    <div class="bar big-bar bar-green" style="height: tqes_heightpx; background: tqes_color;" data-toggle="tooltip" data-placement="top" title="tq_engage_score"></div>
                    <div class="bar bar-yellow" style="height: tqrs_heightpx; background: tqrs_color;" data-toggle="tooltip" data-placement="top" title="tq_relevancy_score"></div>
                    <div class="bar bar-light-green" style="height: sks_heightpx; background: sks_color;" data-toggle="tooltip" data-placement="top" title="s_klout_score"></div>
                    <div class="bar bar-green" style="height: sls_heightpx; background: sls_color;" data-toggle="tooltip" data-placement="top" title="s_legitimacy_score"></div>
                    <div class="bar bar-gray" style="height: tqgs_heightpx; background: tqgs_color;" data-toggle="tooltip" data-placement="top" title="tq_geography_score"></div>
                </div>

                <!-- Labels that correspond with each bar -->
                <div class="bar-labels">
                    <div class="bar-label big-bar-label" data-toggle="tooltip" data-placement="bottom" title="Score">tq_engage_score</div>
                    <div class="bar-label-icon" style="font-size: 12px; color: #000;" data-toggle="tooltip" data-placement="bottom" title="Relevancy">
                        <i class="fa fa-bullseye"></i>
                    </div>
                    <div class="bar-label-icon" data-toggle="tooltip" data-placement="bottom" title="Influence">
                        <i class="fa fa-users"></i>
                    </div>
                    <div class="bar-label-icon" data-toggle="tooltip" data-placement="bottom" title="Legitimacy">
                        <i class="fa fa-check-circle"></i>
                    </div>
                    <div class="bar-label-icon" data-toggle="tooltip" data-placement="bottom" title="Geography">
                        <i class="fa fa-map-marker"></i>
                    </div>
                </div>
            </div>

            <!-- Notes below the bars/graph -->
            <div class="explanations">
                <!-- Note below the bars/graph -->
                <div class="explanation">
                    <div class="explanation-check"><i class="fa fa-first-comment">&nbsp;&nbsp;&nbsp;&nbsp;</i>
                        <div class="explanation-text">
                            comment_one
                        </div>
                    </div>
                </div>

                <!-- Note below the bars/graph -->
                <div class="explanation">
                    <div class="explanation-check"><i class="fa fa-second-comment">&nbsp;&nbsp;&nbsp;&nbsp;</i>
                        <div class="explanation-text">
                            comment_two
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Tweet score -->
        <div class="score-wrapper">
            <div class="score animated">tq_engage_score</div>
        </div>

        <!-- Tweet content -->
        <div class="tweet-content">
            s_twt_text
        </div>

        <!-- Time since tweet was posted -->
        <div class="tweet-time-elapsed">
            s_twt_time
        </div>

        <!-- Area below tweet with reply textarea and buttons -->
        <div class="tweet-reply-section animated">
            <!-- Reply textarea -->
            <textarea class="tweet-reply animated">@s_twt_owner_sn </textarea>

            <!-- Buttons -->
            <div class="buttons animated">
                <!-- Small buttons on top of reply button -->
                <div class="top-buttons">
                    <span class="character-count">
                    </span>
                </div>

                <!-- Reply button -->
                <div class="reply-button">
                    Reply <i class="fa fa-reply"></i>
                </div>
            </div>
        </div>
    </div>
</div>

JavaScript的:

   /**
     * Add a tweet to the feed.
     */
    function _addTweetToFeed(tweet, keywords) {
        /** Get the tweet template */
        var tweetHtml = $('#tweet-template').html();

        // add score heights and colors properties to the tweet
        tweet = _setScoreBars(tweet);

        /** Linkify elements of the tweet */
        tweet.s_twt_text = twitterify(tweet.s_twt_text); // the tweet
        tweet.s_twt_owner_desc = twitterify(tweet.s_twt_owner_desc);

       // fix search terms to be highlighted
        tweet.s_twt_text = _highlightSearchTerms(tweet.s_twt_text, keywords); // the tweet
        tweet.s_twt_owner_desc = _highlightSearchTerms(tweet.s_twt_owner_desc, keywords);

       // change from twitter links to readable links
       tweet = _fixTweetLinks(tweet);

        /** Make numbers readable */
        tweet.s_twt_owner_num_following = abbrNum(tweet.s_twt_owner_num_following, 1);
        tweet.s_twt_owner_num_follower = abbrNum(tweet.s_twt_owner_num_follower, 1);
        tweet.s_twt_owner_num_twt = abbrNum(tweet.s_twt_owner_num_twt, 1);

        /** Loop through the properties of tweet object and populate tweetHtml with them */
        for (var prop in tweet) {
            if (tweet.hasOwnProperty(prop)) {
                tweetHtml = _replaceAll(tweetHtml, prop, tweet[prop]);
            }

            // add comments
            tweetHtml = _addComments(tweet, tweetHtml);

            /** If both location and url are not present, remove the comma */
            if (!(tweet.s_twt_owner_loc && tweet.s_twt_owner_url)) {
                $('#url_comma').html('');
            }
        }
        $('#tweets-container').append(tweetHtml);
    }

2 个答案:

答案 0 :(得分:0)

“有时它的工作原理就像它假设的那样”这一行本身表明您在创建DOM之前运行代码。有时你的浏览器创建dom足够快,并且处理程序正在附加到正文,有时你的javascript首先运行而且它没有附加。将代码包装在此:

$(function(){
    $("body").on("click", ".reply-button", function(){
        alert("test");
    });
});

答案 1 :(得分:0)

  

有没有人知道如何调试这个?

有时最好提取一小部分代码,看看它是否孤立运行。例如:http://jsfiddle.net/6v7z9fak/

JS:

$("body").on("click", ".reply-button", function(){
    alert("test");
});

HTML:

<button type="button" class="reply-button">Reply</button>

正如您所见,它运作正常。因此,您可以确信错误不在单独的代码中。它或者是代码的另一部分,或者部分如何相互作用,或者其他什么。现在慢慢添加更多代码,测试并查看它的中断位置。