Javascript计数器是正确的,但当我使用.click计数器是错误的

时间:2016-04-15 05:40:28

标签: javascript php jquery ajax

我正在练习Ajax并使用JavaScript来处理我的数据响应。我试图像Facebook评论一样,你可以编辑你的评论,如果你是谁做的。在这里,而不是单词“编辑”我把一个计数器号码只是为了看看评论是否正确的计数器,幸运的是一切正常,所以我把代码放在哪里,如果你点击该计数器号码,它将提醒其计数器号码,这就是我的问题来自因为当我点击计数器没有。 “0”它也应警告“0”,但它的警报不同。

任何帮助都将不胜感激。

function handleResponse(){
    var xmlResponse = xmlHttp.responseXML;
    root = xmlResponse.documentElement;
    edit = root.getElementsByTagName("h4");
    counter = root.getElementsByTagName("h3");
    user = root.getElementsByTagName("h1");
    comment = root.getElementsByTagName("h2");
    var counter2 = 0;
    var user1 = "";
    var comment1 = "";
    var edit1 = "";

    for(var i=0;i<counter.length;i++) {
        var counter1 = counter.item(i).firstChild.data;
    }

    while(counter1>=counter2){
        user1 = user.item(counter2).firstChild.data;
        comment1 = comment.item(counter2).firstChild.data;
        edit1 = edit.item(counter2).firstChild.data;

        if( $('.userclass'+ counter2).length ) {
            $('.userclass'+ counter2).text(user1);
            $('.commentclass'+ counter2).text(comment1); 
            if(edit1 == 1){
                $('.editclass'+ counter2).text(counter2);
            } 
        }else{
            $('<div id="DivUser"> </div>').insertBefore("#starting").addClass('userclass'+ counter2);
            $('.userclass'+ counter2).text(user1);
            $('<div id="DivComment"> </div>').insertAfter('.userclass'+ counter2).addClass('commentclass'+ counter2);
            $('.commentclass'+ counter2).text(comment1);
            if( edit1 == 1 ){
                $('<div id="DivEdit"> </div>').insertAfter('.commentclass'+ counter2).addClass('editclass'+ counter2);
                $('.editclass'+ counter2).text(counter2); //here instead of word "edit" i use the variable counter to see if everything goes ok, and everything is ok here.. 
                //return counter2;
                $( ".editclass"+ counter2 ).click(function() {
                  alert( counter2 ); // here where my problem start it alert different number EX: i click 0 but it alert diff no.
                });
            } 
        }
    counter2++;     
    }// end of while
}// end of handleResponse

0 个答案:

没有答案