计算这个非常长的Jquery语句
如何为上述html表计算上述Jquery语句。该语句给出的输出为 在上面的html表中显示。你能告诉我这句话的执行步骤吗计算这个非常长的Jquery语句,jquery,jquery-selectors,Jquery,Jquery Selectors,如何为上述html表计算上述Jquery语句。该语句给出的输出为 在上面的html表中显示。你能告诉我这句话的执行步骤吗 var num = $('#id_seats1').parent().parent().parent().children().last().children().html()*1; 请帮忙 var num = $('#id_seats1').parent().parent().parent().children().last().children().html()*1;
var num = $('#id_seats1').parent().parent().parent().children().last().children().html()*1;
请帮忙
var num = $('#id_seats1').parent().parent().parent().children().last().children().html()*1;
这将选择
元素
$('#id_seats1')
.parent().parent().parent()
这将选择td
,然后选择tr
,然后选择tbody
元素
$('#id_seats1')
.parent().parent().parent()
这将选择tbody元素的最后一个子元素,即tr
.children().last()
这将选择最后一行中所有td
s的html内容
.children().html()
这将最后一行的内容(包含该行中所有单元格内容的字符串)转换为数字
由于第一个单元格包含一个计数器$i
变量,因此该值最终被分配给var num
这将选择
元素
$('#id_seats1')
.parent().parent().parent()
这将选择td
,然后选择tr
,然后选择tbody
元素
$('#id_seats1')
.parent().parent().parent()
这将选择tbody元素的最后一个子元素,即tr
.children().last()
这将选择最后一行中所有td
s的html内容
.children().html()
这将最后一行的内容(包含该行中所有单元格内容的字符串)转换为数字
由于第一个单元格包含一个计数器
$i
变量,即最后分配给var num
的值,因此jQuery语句将不返回表中的行数,而是返回最后一个元素的子元素html的数值等效内容
用于查找表中总行数的jQuery代码段应该是
*1
可以优化为:
var num = $('#id_seats1').parent().parent().parent().children().length - 2;
但理想的解决办法是
var num = $('#id_seats1').closest('tbody').children().length - 2;
jQuery语句将不会返回表中的行数,而是返回最后一个元素的子元素的html的数值等价物的内容 用于查找表中总行数的jQuery代码段应该是
*1
可以优化为:
var num = $('#id_seats1').parent().parent().parent().children().length - 2;
但理想的解决办法是
var num = $('#id_seats1').closest('tbody').children().length - 2;
为什么不数一数行数并减去作为标题的行数(本例中为2)?为什么不数一数行数并减去作为标题的行数(本例中为2)?@Livinston;我有各种各样的想法,比如$(“#tbl1”).attr('rows')。length-2还将给出“tbl1”中的行数。但是想看看上面的优点吗?@Livinston;我有各种各样的想法,比如$(“#tbl1”).attr('rows')。length-2还将给出“tbl1”中的行数。但是想看看上面的优点吗?