【问题标题】:Jquery Index() returns -1Jquery Index() 返回 -1
【发布时间】:2018-05-30 08:02:45
【问题描述】:

我有这个下面的函数,其中 html 是在 Javascript 中创建的。在test(test1) 我正在检查 test1.Test__c 是否为空并更改颜色。 .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;
}

【问题讨论】:

  • 分享任何最能解释的工作示例
  • 请点击&lt;&gt; sn-p 编辑器并创建minimal reproducible example
  • 你能在 jsfiddle 分享你的代码吗...
  • 请在 jsfiddle 工作,我的回答中提供了链接 ..如果你工作,请接受投票
  • 您是否尝试在将 html 添加到 DOM 之前使用 jquery 对某些 html 进行更改?你可以用这种类型的代码做到这一点:var html = '&lt;td...'; $("&lt;div&gt;").html(html); $(div).find(".testcss") ... ?但这对您提供的 sn-p 没有意义(html 变量中没有足够的 html)。

标签: javascript jquery html indexing


【解决方案1】:

在 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());
})

【讨论】:

  • @freedomn-m - 是的,同意实验……现在更新
  • 大家好.. 我知道问题出在哪里了.. 问题出在我写代码的地方没有建表..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多