Javascript jQuery:如何重新计算和命名元素
我有一个表行列表,这些tr由于某种原因对类进行了编号(leg-1、leg-2等)。在此上下文中已经可以删除单个tr 删除一个tr后,我需要重命名剩余的tr 例如: 我有Javascript jQuery:如何重新计算和命名元素,javascript,jquery,html,Javascript,Jquery,Html,我有一个表行列表,这些tr由于某种原因对类进行了编号(leg-1、leg-2等)。在此上下文中已经可以删除单个tr 删除一个tr后,我需要重命名剩余的tr 例如: 我有 现在我删除leg-2。剩下的是: <tr class="leg-1"></tr> <tr class="leg-3"></tr> 现在我想重新命名剩余的tr,使其返回到leg-1和leg-2 如何做到这一点 谢谢你的帮助 编辑: 我忘了提到它可以有多个类为“leg-1”
现在我删除leg-2。剩下的是:
<tr class="leg-1"></tr>
<tr class="leg-3"></tr>
现在我想重新命名剩余的tr,使其返回到leg-1和leg-2
如何做到这一点
谢谢你的帮助
编辑: 我忘了提到它可以有多个类为“leg-1”、“leg-2”的tr。。。因此,正确的开始示例是
<tr class="leg-1"></tr>
<tr class="leg-1"></tr>
<tr class="leg-2"></tr>
<tr class="leg-3"></tr>
<tr class="leg-3"></tr>
现在,当我删除leg-2时,class=“leg-3”的两个tr都必须重命名为class=“leg-2”
对不起,我刚才没提到这个
在行动中看到它
更新
根据您在下面的评论和更新的问题,这里是一个更新的解决方案
var removed = $(".leg-2"),
siblings = removed.nextAll(), // affect only next siblings to removed element
pos = ""; // store current number after "leg-"
removed.remove(); // remove element
siblings.each(function (k) {
$(this).removeClass(function (i, key) {
pos = key.split("-")[1]; // update number after "leg-"
return (key.match(/\bleg-\S+/g) || []).join(' ');
}).addClass("leg-" + (pos - 1)); // add "leg-" plus new position
});
看到它工作
您可以使用
.removeClass()
和.match()
删除以leg
开头的类,然后使用.addClass()
添加类leg
和tr
的索引
看到它工作
尝试此操作,它将重新命名该类:
$(文档).ready(函数(){
$(“#单击”)。单击(函数(){
重新排序();
});
});
函数重新排序(){
$('#myTable>tbody>tr')。每个(函数(索引){
console.log($(this.attr('class','leg-'+(index+1));//从0开始的AVOID
});
}
a1
a2
a3
a4
a5
单击我最直接的建议是:
$('tr').each(function(i) {
// looking for a class-name that starts with (follows a
// word-boundary: \b) leg- followed by one or more numbers (\d+)
// followed by another word-boundary:
var matchedClass = this.className.match(/\bleg-\d+\b/);
// if a match exists:
if (matchedClass) {
// set the node's className to the same className after replacing
// the found leg-X class name with the string of 'leg-' plus the
// index of the current element in the collection plus one:
this.className = this.className.replace(matchedClass[0], 'leg-' + (i + 1));
}
});
$('tr')。每个(函数(i){
var matchedClass=this.className.match(/\bleg-\d+\b/);
//如果存在匹配项:
如果(匹配类){
this.className=this.className.replace(matchedClass[0],'leg-'+(i+1));
}
});代码>
1.
2.
3.
4.
为什么要使用类名来唯一标识元素?这就是id
的作用。无论您试图通过重命名类来做什么,这听起来像是一个XY问题。您最好解释一下为什么需要这样做?操作dom会触发页面的重新布局,如果您经常这样做会降低页面的速度。我建议你考虑另一种方法。也许,如果你能解释一下你想要实现的目标,我们可以想出一个更好的解决方案:)好的,谢谢你所有的解决方案!我忘了提到它可以有多个类为“leg-1”、“leg-2”的tr。。。所以,现在开始的示例是,当我删除leg-2时,class=“leg-3”的两个tr都必须重命名为class=“leg-2”。好的,感谢您迄今为止的解决方案!我忘了提到它可以有多个类为“leg-1”、“leg-2”的tr。。。因此,正确的开始示例是,当我删除leg-2时,两个带有class=“leg-3”的tr都必须重命名为class=“leg-2”…:-/对不起,我刚才没提到这个!!然后,您可能需要编辑您的问题,以包括相关的详细信息,因为此“行计数”似乎不是行计数。但是,考虑到您对原始问题的答案,我建议您再问一个问题,并明确说明您的要求(否则,看起来您武断地改变了您的问题,并使发布的答案无效)。+1好的,您的解决方案与我想要的不完全一样(因为我没有问完整的问题…真丢脸),但不管怎样,它还是很有帮助的!!好吧,我很高兴能提供帮助,即使只是以一种小而不完整的方式。好的,谢谢你迄今为止的解决方案!我忘了提一下,它可以有多个tr与类“leg-1”,“leg-2”…因此,正确的开始示例是现在当我删除leg-2时,class=“leg-3”的两个tr都必须重命名为class=“leg-2”…:-/很抱歉,我之前没有提到这一点!!@SchweizerSchoggi我很高兴听到这样的消息:)好的,感谢您迄今为止的解决方案!我忘了提到它可以有多个类为“leg-1”、“leg-2”的tr。。。因此,正确的开始示例是,当我删除leg-2时,两个带有class=“leg-3”的tr都必须重命名为class=“leg-2”…:-/对不起,我刚才没提到这个!!
$("tr").each(function (k) {
k++;
$(this).removeClass(function (i, key) {
return (key.match(/\bleg-\S+/g) || []).join(' ');
}).addClass("leg-" + k);
});
$('tr').each(function(i) {
// looking for a class-name that starts with (follows a
// word-boundary: \b) leg- followed by one or more numbers (\d+)
// followed by another word-boundary:
var matchedClass = this.className.match(/\bleg-\d+\b/);
// if a match exists:
if (matchedClass) {
// set the node's className to the same className after replacing
// the found leg-X class name with the string of 'leg-' plus the
// index of the current element in the collection plus one:
this.className = this.className.replace(matchedClass[0], 'leg-' + (i + 1));
}
});