2017-10-20 5 views
0

要するに、私が直面している問題は、空の値を持たないクラス開始タイムコードでTDに到達するまで前のTRをスキップし続けることができないということです。ここでjQueryがTDに到達するまでの前のTRを見つけるDATA

タイムコードのない空の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); 
    } 
    });  
}); 

しかし、その開始タイムコード。ご覧のとおりです。 https://jsfiddle.net/jLx1sfx8/5/

のRESULT NEED: 私はjQueryのは、それがTDに達するまで、前の行をスキップしておきたいが開始タイムコードデータを持っている含まれています。

注:私は:empty:has、untilAllなどを試しましたが、何らかの形でそれを達成できなかったり、正確には正確な構文がわかりません。

答えて

2

順にtr Sを確認するwhileループを使用:

https://jsfiddle.net/62eo08tt/

これは、中間の割り当て 1つのおいしく長jQueryの鎖と で達成することができない
$('#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

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

関連する問題