Jquery获取元素的最新id并提取数字

时间:2022-11-27 11:24:56

I have something like this:

我有这样的事情:

<div id="row1">Some content</div>
<div id="row2">Some content</div>
<div id="row3">Some content</div>
<div id="row4">Some content</div>
<div id="row7">Some content</div>
<div id="row15">Some content</div>
<div id="row915">Some content</div>
<div id="row919">Some content</div>

Rows are actually pulled from PHP array and now I need to extract last row's number e.g. in this case that would be 919. (so I can add +1 to row id when I use append to generate more rows via jQuery).... Any ideas ?

行实际上是从PHP数组中提取的,现在我需要提取最后一行的数字,例如在这种情况下,这将是919.(因此,当我使用append通过jQuery生成更多行时,我可以向行id添加+1)....任何想法?

5 个解决方案

#1


1  

If you cant do what Jon have suggested you can try this:

如果你不能做乔恩建议你可以试试这个:

var max = -Infinity;
$('div')​​.each(function () {
    var match = this.id.match(/^row([0-9]+)$/)
    if (max < match[1]) {
        max = match[1];            
    }
});

$('#row' + max).html('Max!');
​

Here is an example fiddle: http://jsfiddle.net/HTkkb/

这是一个例子小提琴:http://jsfiddle.net/HTkkb/

The script above finds the maximum number for postfix of all div's ids and gets the element with id row + max

上面的脚本找到所有div的id的后缀的最大数量,并获取id row + max的元素

#2


5  

Id suggest rather than doing this you keep a variable with the last value. When you then append you can alter this value. This saves getting the id and parsing it.

我建议而不是这样做,你保留一个变量与最后一个值。当你追加时,你可以改变这个值。这节省了获取id并解析它。

#3


2  

assuming they're in the same div, and assuming that the row919 is the last element, you can do something like this:

假设它们在同一个div中,假设row919是最后一个元素,你可以这样做:

var last_element_id = $('.parentDiv').last().attr('id');
var number = last_element_id.replace('row','');

#4


0  

Use :last

alert($('div:last').attr('id').split('row')[1]);

Fiddle example jsfiddle

小提琴示例jsfiddle

#5


0  

It easier to use the data- attribute and a class:

更容易使用data-属性和类:

<div class="row" data-id="1">Some content</div>
<div class="row" data-id="2">Some content</div>

As the other answers have said, storing a variable and incrementing would be better to get the next increment. However you should still consider the data-id structure and not trying to parse it out of the id attribute which has a label wihthin it.

正如其他答案所说,存储变量和递增会更好地获得下一个增量。但是,您仍然应该考虑data-id结构,而不是尝试从id属性中解析它,该属性具有标签。

#1


1  

If you cant do what Jon have suggested you can try this:

如果你不能做乔恩建议你可以试试这个:

var max = -Infinity;
$('div')​​.each(function () {
    var match = this.id.match(/^row([0-9]+)$/)
    if (max < match[1]) {
        max = match[1];            
    }
});

$('#row' + max).html('Max!');
​

Here is an example fiddle: http://jsfiddle.net/HTkkb/

这是一个例子小提琴:http://jsfiddle.net/HTkkb/

The script above finds the maximum number for postfix of all div's ids and gets the element with id row + max

上面的脚本找到所有div的id的后缀的最大数量,并获取id row + max的元素

#2


5  

Id suggest rather than doing this you keep a variable with the last value. When you then append you can alter this value. This saves getting the id and parsing it.

我建议而不是这样做,你保留一个变量与最后一个值。当你追加时,你可以改变这个值。这节省了获取id并解析它。

#3


2  

assuming they're in the same div, and assuming that the row919 is the last element, you can do something like this:

假设它们在同一个div中,假设row919是最后一个元素,你可以这样做:

var last_element_id = $('.parentDiv').last().attr('id');
var number = last_element_id.replace('row','');

#4


0  

Use :last

alert($('div:last').attr('id').split('row')[1]);

Fiddle example jsfiddle

小提琴示例jsfiddle

#5


0  

It easier to use the data- attribute and a class:

更容易使用data-属性和类:

<div class="row" data-id="1">Some content</div>
<div class="row" data-id="2">Some content</div>

As the other answers have said, storing a variable and incrementing would be better to get the next increment. However you should still consider the data-id structure and not trying to parse it out of the id attribute which has a label wihthin it.

正如其他答案所说,存储变量和递增会更好地获得下一个增量。但是,您仍然应该考虑data-id结构,而不是尝试从id属性中解析它,该属性具有标签。