我使用的是一个jquery插件,它可以创建要导航的标签,除了最后一个硬编码的标签外,所有的标签都是动态加载的。我仍然想像往常一样导航,这样标签就需要在标签之前有标签的id +1。
以下是使选项卡起作用的两个元素:
<a href="#step-10" class="nav-link">Vul je keuze aan</a>
<div id="step-10" class="" style="">例如,上面有10个步骤/选项卡,那么硬编码的步骤需要:
<a href="#step-11" class="nav-link">Vul je keuze aan</a>
<div id="step-11" class="" style="">我该怎么做呢?我不能简单地将它解析为一个数字然后加1,因为这个数字和step-一起在一个字符串中。如何从该字符串中删除数字,增加数字,然后将新数字放回原处?
发布于 2019-12-11 17:05:32
这个问题的答案。假设我将id分配给了一个How can I remove the number from that string, increase it and put the new number back?
var id = "step-11"
var splittedId = id.split("-"); //["step", "11"]
var no = parseInt(splittedId[1]); // 11
var step = splittedId[0]; // "step"
var newId = step+"-"+(++splittedId[1]); // "step-12"发布于 2019-12-11 17:17:14
您可以做的一件事是将所有id为step-的div进行计数,并将此计数加1,如下面的代码所示
const test = document.querySelectorAll("div[id*='step-']");
document.getElementsByClassName('testclass')[0].id='step-'+(test.length+1)https://stackoverflow.com/questions/59281912
复制相似问题