Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
排序,element.firstChild.nextSibling.firstChild.data;JavaScript,表_Javascript_Sorting_Dom - Fatal编程技术网

排序,element.firstChild.nextSibling.firstChild.data;JavaScript,表

排序,element.firstChild.nextSibling.firstChild.data;JavaScript,表,javascript,sorting,dom,Javascript,Sorting,Dom,//已解决 我试图在DOM树中导航并对元素进行排序 <table> <tr> <td>El</td> <td>B</td> <---------Take this as a criterion </tr> <tr> <td>El</td> <td>A</td> <------------- same </tr> </tabl

//已解决
我试图在DOM树中导航并对元素进行排序

<table>
<tr>
<td>El</td>
<td>B</td> <---------Take this as a criterion
</tr>
<tr>
<td>El</td>
<td>A</td> <------------- same
</tr>
</table>


var compare=function(x, y) {
    var xValue=x.innerHTML;
    var yValue=y.innerHTML;
    if(xValue==yValue) return 0;
    else if(xValue>yValue) return 1;    
    else return -1; 
}
但是,没有错误,但是我的程序没有反应。我是否使用了正确的命令/element.firstChild.nextSibling.firstChild.data->(请参阅代码中if下的注释)/


PS:如果有必要的话,我不允许使用库。

我建议您备份并找到一个好的算法来对表行进行排序。因为这看起来像是家庭作业,所以我将在不给出确切代码的情况下推荐算法

我见过多次使用的通用算法如下所示:

  • 构建一个对象数组,其中数组中的每个对象都包含一行DOM元素引用和要排序的内容

  • 使用自定义排序函数对该数组进行排序。由于内容与对包含行的DOM引用一起位于对象中,因此这将两者一起排序

  • 现在您已经有了一个排序数组,只需按照排序数组的顺序将每一行依次重新插入到表中即可。这会移动整行元素,实际上不会重新分配内容


  • 下面是另一个答案的相关示例:

    这是你的家庭作业,对吗?请在问题中描述你试图解决的问题。您是否正在尝试“根据表中特定列中的内容对表中的行进行排序”?如果是这样的话,请把它放在问题的顶部附近。如果没有,那么请准确描述您问题中的任务。@ThomasLandauer是的,这就是我不能使用库的原因,但是,我不是要求别人编写它,我要求的是一个具体的命令,在我的案例中是element.firstChild.nextSibling.firstChild.data;我可以这么做你的
    compare()
    函数在哪里?它有什么作用?另外,请描述在
    cattosort.sort()
    @jfriend00比较用于排序之后,您在第三个
    for
    循环中尝试执行的操作,因此使用cattosort.sort(…)我对数组进行排序,我首先在其中复制了需要排序的内容;在我的最后一个for循环中,我做了如下操作:对于数组中的所有元素cattosort{检查未排序数组中的哪个位置k是元素,即排序数组中的位置I,将元素k移动到位置I->element k sorted…对于每个元素如此
    var row_t=new Array();
              for(i=0;i<row_tab.length;i++){
                  row_t[i]=row_tab[i]; //2.
              }
    
        var cattosort=new Array(); //Array for the second td-elements
        for(i=0;i<row_tab.length; i++){
            cattosort[i]=row_tab[i].firstChild.nextSibling;
        }    
              cattosort=cattosort.sort(compare);
    
          for(i=0;i<cattosort.length; i++){
              for(var k=0; k<row_t.length; k++){
            if(cattosort[i].firstChild.data==row_t[k].firstChild.nextSibling.firstChild.data){ 
    //Here should be the mistake in the navigation,but formal it
    //seems true for me (row_t[k].firstChild.nextSibling.firstChild.data->
    //the text of the next sibling of the first child->the text of the 2. element    
    
           z=row_tab[i];
           row_tab[i]=row_t[k];
           row_t[k]=z;
        }
          } 
          }