Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用jquery,当元素不是';t按连续顺序排列?_Javascript_Jquery_Arrays - Fatal编程技术网

Javascript 使用jquery,当元素不是';t按连续顺序排列?

Javascript 使用jquery,当元素不是';t按连续顺序排列?,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有一组链接,如下例所示。链接的ID是连续编号的,但链接本身不是连续顺序。我知道如何返回所单击链接的href和ID,但我还想获得具有下一个连续ID的链接的href,当它成为所选元素时,获取下一个,以此类推。我知道当元素以连续的顺序排列时如何做到这一点,但当它们是这样的时候就不行了。 任何帮助都将不胜感激 我的HTML <div id="container"> <div class="group"> <a class="clickable" id="itemid-0

我有一组链接,如下例所示。链接的ID是连续编号的,但链接本身不是连续顺序。我知道如何返回所单击链接的href和ID,但我还想获得具有下一个连续ID的链接的href,当它成为所选元素时,获取下一个,以此类推。我知道当元素以连续的顺序排列时如何做到这一点,但当它们是这样的时候就不行了。 任何帮助都将不胜感激

我的HTML

<div id="container">

<div class="group">
<a class="clickable" id="itemid-01" href=""></a>
<a class="clickable" id="itemid-04" href=""></a>
</div>

<div class="group">
<a class="clickable" id="itemid-02" href=""></a>
<a class="clickable" id="itemid-05" href=""></a>
</div>

<div class="group">
<a class="clickable" id="itemid-03" href=""></a>
<a class="clickable" id="itemid-06" href=""></a>
</div>

</div>

如果我对这个问题理解正确的话,这个就可以了。我添加if语句只是为了在数字小于10时捕捉尾随的零。对于大于100的数字,您必须执行类似操作

$(document).ready(function(){
    $('.clickable').click(function(e){
    e.preventDefault();
    var this_ID = $(this).attr('id');
    var id_pieces = this_ID.split('-');
    var currentId = parseInt(id_pieces[1]);

    var next_ID = '';
    if(currentId>=10){
        next_ID = id_pieces[0]+'-'+(currentId+1);
    }else{
        next_ID = id_pieces[0]+'-0'+(currentId+1);
    }

    console.log(next_ID);

    });
    });

我不确定你的标记的范围是关于你计划在屏幕上显示多少元素。不管是什么情况,考虑到你的设置方式,你可以这样做:

var thisId = $(this).attr('id');
var nextId = null;

//use some regex to get the id from the end of the string
var matches = thisId.match(/\d+$/);

//if it finds a matching integer...
if (matches) {

    //parse out the integer and increment it by 1
    var nextIdInt = parseInt(matches[0], 10);
    nextIdInt++;

    //build the next id string:
    //if less than 10, it addes a leading zero, otherwise just leaves it as is
    nextId = (nextIdInt < 10) ?  "itemid-" + "0" + nextIdInt : "itemid-" + nextIdInt;
}

if (nextId) {
    //do something here with the next id if it is not null
}
如果你不想让它可读,你只需要下一个id:

var thisId = $(this).attr('id');
var nextId =  $('a.clickable[data-index="' + (parseInt($(this).attr('data-index'), 10) + 1) + '"]').attr('id');

这里有一个

你需要下一个id的数字还是需要遍历DOM到下一个id?嗨,Matt,我需要下一个id的数字。我提供的答案应该会起作用,也会给你一些我认为会让你的生活更轻松的选择:)这个答案确实有效,但是我认为字符串操作有点可疑,如果你想这样做,regex是一个更安全的选择。嗨,马特,谢谢你的解释,自定义属性是一个很好的选择。我尝试了一下,但只能得到一个链接。我需要它按顺序通过所有链接。有点像灯箱中的“下一步”按钮,需要更好的解释。让我确保我理解:那么,我提供的小提琴是否按照您期望的方式工作?我的理解是,只要点击一个给定的锚,您就需要根据序列获得下一个(并且只有下一个)ID。小提琴就是这样工作的。也许我不太明白你在找什么?或者如果你想发布你的代码,我可以看看!
<a class="clickable" id="itemid-01" href="" data-index="1"></a>
var thisId = $(this).attr('id');
var nextId = null;

//increment the index and store it in a variable
var nextIndex = parseInt($(this).attr('data-index'), 10) + 1;

//get the next element
var nextElement = $('a.clickable[data-index="' + nextIndex + '"]');

//probably smart to make sure we found something...
if (nextElement.length > 0) {

    //if you want the id
     nextId = nextElement.attr('id');

}
var thisId = $(this).attr('id');
var nextId =  $('a.clickable[data-index="' + (parseInt($(this).attr('data-index'), 10) + 1) + '"]').attr('id');