单击拼接阵列 - javascript

时间:2014-10-02 07:49:09

标签: javascript jquery arrays array-splice

我有一种类似Facebook的新闻源墙,你可以创建帖子。我希望当我点击帖子中的glyphicon删除那个帖子时。

代码是:

function loadPosts(){
    $('#allPosts').empty();
    for (var i = 0; i < aPosts.length; i++){
        var postData = aPosts[i];
        var comments ="";
        for (var j = 0; j < postData.comments.length; j++ )
        {
            comments += '<p class="myComment">' + postData.comments[j].from + '<strong>says: </strong>'+ postData.comments[j].text +'</p>';
        }
        $('#allPosts').prepend('<div data-postId="'+i+'"class="wrapPost"><span class="glyphicon glyphicon-remove"></span>'  +
            postData.from +' '+''  +
            '<strong>wrote :</strong>'+
            '<li>' + postData.text + '</li>' + comments +
            '<input type=text class=commentBox placeholder=Comment..>' +
            '<button data-postId="'+i+'" class="postComment">Post</button></div>');

        $('.glyphicon-remove').click(function() {
            var postId = $(this).parent().attr('data-postId');
            console.log(postId);
            aPosts.splice(i, 1);

        });
    }
}

当我登录日志“postId”时,它告诉我我至少点击了正确的帖子,但它不会从localStorage中删除它。

0 个答案:

没有答案