Get tbody next to one below the current tbody

断了今生、忘了曾经 提交于 2020-01-03 05:28:50

问题


I have a SharePoint table and would like to get the tbody of the table right one below the tbody with groupstring.

Sample table

<tbody groupstring=“%3B%23Application%3B%23”>
<tr><td> : Application </td></tr></tbody>
<tbody></tbody>
<tbody>
<tr><td><a href=“https://link1.com”>Link 1</a></td></tr>
<tr><td><a href=“https://link3.com”>Link 3</a></td></tr>
</tbody>

I will be getting another link Link2 from Json and dynamically inserting it between Link1 and Link3 with Jquery

var addRow=function(url,displayName){
Var counter=0;
$(‘tbody[groupstring=“%3B%23Application%3B%23”]’).closest(‘tbody’).find(‘td’).each(function(){
counter++;
If($(this).find(‘td:eq(0)’).text() > displayName){
$(this).before(makeRow(url,displayName));
} else {
If($(this).find(‘td:eq(0)’).text() === displayName){
$(this).html(makeRow(url,displayName));
Return false;
}}
If($(this).closest(‘table’).find(‘td.md-vb2’).length === counter){
$(this).parent().after(makeRow(url,displayName));
} 
});
};

Var makeRow = function(url,displayName){
return (‘<tr><td><a href=“‘ + url + ‘”>’+displayName+’</a></td></tr>’);
};

I am trying to add the dynamic Link2 between Link1 & Link3 as anchor by placing in between or replace if already exists by addRow function. My problem is that I can insert in the tbody after the tbody with groupstring but I want to insert in tbody one after the blank tbody. Can somebody help modify:

 $(‘tbody[groupstring=“%3B%23Application%3B%23”]’).closest(‘tbody’).find(‘td’).each(function(){

To access/get the tbody* one after the blank tbody? I even tried:

$(‘tbody[groupstring=“%3B%23Application%3B%23”]’).closest(‘tbody’).next(‘tbody’).find(‘td’).each(function(){

And

$(‘tbody[groupstring=“%3B%23Application%3B%23”]’).closest(‘tbody’).closest(‘tbody’).find(‘td’).each(function()

But it did not work. Any help is much appreciated.


回答1:


$('tbody[groupstring]').nextAll('tbody:eq(1)') should do it.

Demo:

$(function() {
  var $target = $('tbody[groupstring]').nextAll('tbody:eq(1)');
  console.log($target.html());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
  <tbody groupstring="%3B%23Application%3B%23">
    <tr>
      <td>Application</td>
    </tr>
  </tbody>
  <tbody></tbody>
  <tbody>
    <tr>
      <td><a href="https://link1.com">Link 1</a></td>
    </tr>
    <tr>
      <td><a href="https://link3.com">Link 3</a></td>
    </tr>
  </tbody>
</table>

Also note that groupstring is not a valid HTML attribute.



来源:https://stackoverflow.com/questions/53162864/get-tbody-next-to-one-below-the-current-tbody

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!