JavaScript / Jquery重复功能无法正常运行

时间:2017-08-03 06:27:42

标签: javascript jquery arrays

茫然地盯着我的屏幕几个小时,并且认为最好能看出是否有人可以确定我的功能究竟出了什么问题。我基本上有两个重复的功能(初始工作,第二个已被复制以执行相同的功能,但检索单独的实体,但根本没有运作)。我似乎无法确定我在第二个函数中是否错误引用某些内容,或者某些内容是不合适的,所以我们非常感谢您的帮助。

我项目的初始部分引用了这个数组:

var index;    

var topics = [      
    {title: "Football Topics", followers: 280, articles: 5, posts: [
        {postId: 101, contents: "Dummy content", author: "Dummy content", replies:[
            {replyId: 1, contents: "Dummy content", author: "Dummy content"},
        ]},
    ]}
]; 

以下函数是调用show__函数的函数,在上一个成功显示showSingleTopic()的函数中调用了topicOnClick,所以我假设topicOnClick2()是问题

function topicOnClick (node, topic){
    'use strict';
    node.on("click", function() {
        showSingleTopic(topic);
    });
}

function topicOnClick2 (node, topic){
    'use strict';
    node.on("click", function() {
        showComments(topic);
    });
}





function getSingleTopic(someTopics, topicTitle) {
    return someTopics.find(function (topic) {
        return topic.title === topicTitle;
    });    
}

function getSingleComment(moreTopics, commentTitle) {
    return moreTopics.find(function (topic) {
        return topic.author === commentTitle;
    });    
}

//showSingleTopic

function showSingleTopic (topicDetails){

    'use strict';

    $("#browsePage").hide();
    $("#individualForumPage").show();

    var page = $("#individualForumPage");
    //page.append("<h1>"topicDetails.title"</h1>")

    var individualTopicTable = $("<table class='forumTable'><tr><th>Contents</th><th>Author</th></tr></table>");

    //Looping all topics in variable "topics"
        for (index in topicDetails.posts) {
                        // let's save the current "post" object into a variable for easy-access
                        var post = topicDetails.posts[index]; // we get one post at a time, and put it into "post" variable
        //console test
        console.log(post.postId);
        var row = $("<tr></tr>");
        row.append("<td>" + post.contents + "</td>");
        row.append("<td>" + post.author + "</td>");

        topicOnClick2(row, topics[index]);


        individualTopicTable.append(row);

        }

    page.html(individualTopicTable);



}










    function showComments (commentDetails){

        'use strict';
        alert(commentDetails.author);

        $("#browsePage").hide();
        $("#individualForumPage").hide();
        $("#commentsPage").show();

        var page = $("#commentsPage");
        //page.append("<h1>"topicDetails.title"</h1>")

        var commentTable = $("<table class='forumTable'><tr><th>Author</th><th>Comments</th></tr></table>");



        //Looping all topics in variable "topics"
            for (index in commentDetails.replies) {
                            // let's save the current "post" object into a variable for easy-access
                            var reply = commentDetails.replies[index]; // we get one post at a time, and put it into "reply" variable

            var row = $("<tr></tr>");
            row.append("<td>" + reply.author + "</td>");
            row.append("<td>" + reply.contents + "</td>");


            //topicOnClick(row, topics[index]);


            commentTable.append(row);

            }
        //========================================================================================================================================================
    // End of referenced source code
    //========================================================================================================================================================

    page.html(commentTable);


    }

0 个答案:

没有答案