Id clone只克隆我点击的第一个项目而不是其他项目

时间:2015-04-19 17:12:21

标签: jquery

function publishHandler(){
var publish = new post();
publish.set("Title", storyTitle.value);
publish.set("User", Parse.User.current());
publish.set("Author", aruthor.value);
publish.set("Story", storyBox.value);
publish.save({
success:function(){
publishWindowHandler();
storyForm.reset();
 },
error:function (error){
alert("sorry your story was not published");
}
});
}




var  publishWindowHandler function(){

var query = new Parse.Query('Post');
console.log(currentUser); 
query.equalTo("User", currentUser);
query.include("User");
query.descending("createdAt")
console.log(user.get('username'));
// query.limit(4);
query.find({
success:function(results){
    document.getElementById("publishCenter").textContent = "";

    for(var i =0; i < results.length; i++){

    var userPost = results[i];



    var authorTitle = document.createElement("p");
    var newPost = document.createElement("P");
    var title = document.createElement("P");
    var userLabel = document.createElement("p");
    var postId = userPost.id;

    var postBtn = document.createElement("INPUT"); 
    postBtn.setAttribute("Type", "button");
    postBtn.setAttribute("value", "Publish");
    title.textContent = "Story: " + userPost.get("Title");
    authorTitle.textContent = "Author: " + userPost.get("Author");
    newPost.textContent = userPost.get("Story");
    userLabel.textContent = "Published by: " +                    userPost.get("User").get("username");

    var postWrapper = document.createElement("DIV");
    postWrapper.className = "postWrapper";
    postWrapper.id = postId;

    document.getElementById("publishCenter").appendChild(postWrapper);
    postWrapper.appendChild(title);
    postWrapper.appendChild(authorTitle);
    postWrapper.appendChild(newPost);
    postWrapper.appendChild(userLabel);
    postWrapper.appendChild(postBtn);

    postBtn.addEventListener("click", publicViewHandler);

    function publicViewHandler(){
    $("#" + postId).clone().appendTo(".storyBoard");

  };

我已经抛出了一个演示域,以防有人想看到我真正在谈论的内容...... http://subdomain.jason-c.com/

测试登录是用户kio,密码是kio ...当你登录我发布了一些lorem ipsum我试图完成的是当我点击我的故事时我想要选择故事,但它只是克隆相同我点击的一个没什么特别的......对我做错了什么的澄清会很棒

1 个答案:

答案 0 :(得分:0)

这是因为您在点击处理程序中使用$("#" + postId)。您不能在此类处理程序中使用变量postId,因为侦听器中有不同的上下文。通过这一点修改,您的代码可以完美运行。

function publicViewHandler(){
    $(this).parent('.postWrapper').clone().appendTo(".storyBoard");
};