2017-10-20 32 views
0

總之,我現在面臨的問題是不能夠保持跳過前面的TR,直到我達到了TD帶班開始,時間碼,沒有空值。jQuery來查找以前的TR,直到它到達一個TD包含數據

下面是實際的代碼工作正常,如果有時間碼的沒有空的TD: https://jsfiddle.net/jLx1sfx8/1/

$(document).ready(function(e) { 
    $('#check').click(function(e) { 
    var activeExists = $('#caption-table .active-narrative').length; 
    if (activeExists > 0) { 
     var totalRows = $('#caption-table tr').length; 
     var startTimeThisRange = $('.active-narrative .start-timecode').text(); 
     var startTimeNextRange = $('.active-narrative').closest('tr').prev().find('.start-timecode').text(); 
     alert(startTimeNextRange); 
    } 
    });  
}); 

但是,當有上一行的時間碼的空TD那麼它不工作得到它的開始時間碼。正如你在這裏看到的。 https://jsfiddle.net/jLx1sfx8/5/

結果需要: 我想jQuery來保持跳過前面的行,直到它到達的TD包含啓動時間碼具有數據。

注:我試過:empty,:has,直到all等,但以某種方式無法實現它或最可能我完全不知道確切的語法。

回答

2

使用while循環以便檢查tr S:

https://jsfiddle.net/62eo08tt/

$('#check').click(function(e) { 
    var activeExists = $('#caption-table .active-narrative').length; 

    if (activeExists > 0) { 
     var totalRows = $('#caption-table tr').length; 
     var startTimeThisRange = $('.active-narrative .start-timecode').text(); 

     var $prevTr = $('.active-narrative').closest('tr').prev(); 
     while ($prevTr.length && $.trim($prevTr.find('.start-timecode').text()) == '') { 
     $prevTr = $prevTr.prev(); 
     } 

     var startTimeNextRange = $prevTr.find('.start-timecode').text(); 
     alert(startTimeNextRange); 
    } 
    }); 
0

這可以與一種可口長jQuery的鏈和可以實現沒有中間分配

var startTimeNextRange = $('.active-narrative') 
.closest('tr') // active-narrative's table row 
.prevAll() // all previous TRs (in same tbody) 
.find('.start-timecode') // Within those TRs find all SPANs with class="start-timecode" 
.filter(function() { // filter to eliminate empty SPANs 
    return !!this.innerText; // return `false` for empty SPANs, `true` for populated SPANs 
}) 
.eq(-1) // select the last populated SPAN. 
.text(); // grab its text 

if(startTimeNextRange) { // need test here because `startTimeNextRange` will be falsy if no qualifying SPANs were found. 
    alert(startTimeNextRange); 
} else { 
    alert('not found'); 
} 

demo

相關問題