Jquery Index()返回-1

时间:2017-12-16 07:58:22

标签: javascript jquery html indexing

我有以下功能,其中html是在Javascript中创建的。在test(test1)我正在检查test1.Test__c是否为null并更改颜色。 .table-striped > tbody > tr.testcss“ - >正在更改行颜色。

但我得到了

var id=$( ".table-striped > tbody > tr.testcss" ).index() --> -1.

我得到的是在我的情况下,那时候因为索引返回-1而没有构建行。

任何人都可以告诉你如何解决这个问题。

function ttttttt(test1) {
    var styleClass = "testcss";

    var html = '<tr class="' + styleClass + '" id="' + test1.Id +'">';
    return html + test(test1);
}


function test(test1) {
    var html = '<td class="cell col-md-2">' +'</td>';

    var opt = test.Test__c;
    if(oppchek === undefined){
        var id=$( ".table-striped > tbody > tr.testcss" ).index();
       );
        ++id;
    } else {
    }
    return html;
}

1 个答案:

答案 0 :(得分:0)

在jsFiddle工作:https://jsfiddle.net/pranayamr/rkcxqyw9/3/

alert($('table > tbody > tr.testcss').index());
//if you want to work on all tr having `testcss` than do like this 
$('table > tbody > tr.testcss').each(function(){
       alert($(this).index());
})