重建DIV id [英] Rebuild DIV id's

查看:63
本文介绍了重建DIV id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一块输出div的php(取决于数组中的数量)并指定带有id的div(即div_1,div_2)等等。

I have a piece of php that outputs divs (depending on how many is in the array) and assigns that div with an id (i.e div_1, div_2) etc

我还设置了一个隐藏字段,其中包含输出了多少div的数量(divcount)

I also set a hidden field with the count of how many divs were outputted (divcount)

所以假设我有5个div然后div的id将是

So lets say I have 5 divs then the id's of the div's will be


  • div_1,

  • div_2,

  • div_3

  • 等等

  • 并且隐藏字段(divcount)将设置为5

  • div_1,
  • div_2,
  • div_3
  • etc etc
  • and the hidden field (divcount) will be set to 5

如果div_2被删除,我将divcount隐藏字段设置为4.

If div_2 gets deleted, I set the divcount hidden field to 4.

我现在需要做的是重建另一个div的id是:

What I need to do now is rebuild the other div's id's to be:


  • div_1,

  • div_2,

  • div_3 ,

  • div_4,
    但目前我留下了div_1,div_3,div_4,div_5

  • div_1,
  • div_2,
  • div_3,
  • div_4, but currently I am left with div_1, div_3, div_4, div_5

我想我需要一个循环遍历每个div,并为该循环的值分配THAT div的id

I guess I need a loop to loop through each div and assign the id of THAT div the value of the loop


  • div_1 id变为div_1

  • div_3 id变为div_2

  • div_4 id变为div_3

  • div_5 id变为div_4

  • div_1 id becomes div_1
  • div_3 id becomes div_2
  • div_4 id becomes div_3
  • div_5 id becomes div_4

任何人都可以建议如何做到这一点吗?

Can anyone advise on how to do this?

谢谢提前!

推荐答案

您可以使用每个方法。

$('div[id^="div_"]').each(function(i){
   this.id = 'div_'+(++i)
})

小提琴

这篇关于重建DIV id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆