查找父('TD')ID循环不显示整个id

时间:2012-05-11 01:04:52

标签: arrays loops element parent-child

我遇到了一些问题,要求让父节点id完整显示。

我正在使用一个循环来检测表中的div元素,然后我找到了它的父TD节点,而在另一个变量中,它得到了id。

我的问题是,在这个循环中,我在数组内部使用此TD id构建一个变量,当它打印时,第一次只显示第一个字符,然后第二次只显示第二个字符之后的任何时候都会出现空白。

如果我从变量中删除数组,它将打印整个id,但每次循环运行时它会将旧值重命名为最新值。

有人可以帮我解决这个问题吗?

感谢任何帮助,提前致谢。

继承人写作的功能

test.save = function () {

var cell = rd = REDIPS.drag,
    page1a = document.getElementById(redips.p1a),
    divs = TableID.getElementsByTagName('DIV'),
    rd.find_parent('TD', rd.obj),     //parent cell
    st = cell.id,                           //parent cell ID
    st,                     
    id,                                     //div id
    cid,                                   //cell id    
    ccid, 
    concatenator,                         // concatenate oDiv ID
    query = '',
    div,                                // current DIV element
    i;                                  // loop variable

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

    // set current DIV element
    div = divs[i];

    // set current TD ID
    cid = st[i];

    if (div.className.indexOf('drag') > -1) {

                    //takes two characters off of the DIV ID only
        concatenator = div.id.substring(0, div.id.length - 2);

        // creates the query through the loop
            query += cid + '=' + concatenator + ' ';
        }
}

     if (query.length > 0) {
     document.getElementById('message').innerHTML = query;
}
    };

表示使用具有TD ID的数组的版本的链接:http://stevenschemers.com/beta1/

并且指向不使用TD ID数组的版本的链接:http://stevenschemers.com/beta2/

我也注意到,如果运行两次似乎是循环,因为'i'将计数0,2,4,6或有时1,3,5,7

1 个答案:

答案 0 :(得分:0)

// loop goes through each DIV element collected on the left side of page 1
for (i = 0; i < divs.length; i++) {
    // set current DIV element
    div = divs[i];

    if (div.className.indexOf('drag') > -1) {
        // sets current cell ID for dropped div
        cid = rd.find_parent('TD', div).id;

        concatenator = div.id.substring(0, div.id.length - 2);
        // creates the query through the loop
            querya += cid + '=' + concatenator + '&';
            qc++;
        }
}