2015-10-16 19 views
0

根據下面的示例,我有一組鏈接。鏈接的ID是連續編號的,但鏈接本身不是連續的順序。我知道如何返回被點擊的鏈接的href和ID,但我也想獲得下一個連續ID的鏈接的href,並且當它變成選定的元素時,獲取下一個等等。我知道如何做到這一點,當元素是連續的順序本身,但不是當他們是這樣的。 任何幫助將不勝感激。使用jquery,當元素不是連續的順序時,如何返回具有下一個連續ID的元素?

我的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> 

我的腳本

$(document).ready(function(){ 
$('.clickable').click(function(e){ 
e.preventDefault(); 
var this_href = $(this).attr('href'); 
var this_ID = $(this).attr('id'); 
var next_ID = //this is the part I can't work out 
}); 
}); 
+0

您是否需要數字化下一個ID,還是需要遍歷DOM到下一個ID? –

+0

嗨馬特,我需要數字的下一個ID。 –

+0

我提供的答案應該工作,也給你一些替代品,我認爲這會讓你的生活更輕鬆:) –

回答

0

如果我理解正確的問題,這應該這樣做。如果數字小於10,我添加了if語句來追蹤尾部零。對於大於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); 

    }); 
    }); 
+0

嗨,何塞,這確實有把戲。感謝堆。 –

+0

這個答案確實有用,但我認爲字符串操作有點可疑,如果這就是你想要做的,正則表達式是一個更安全的選擇。 –

0

我不確定你的標記的範圍是關於你計劃在屏幕上有多少元素。無論是哪種情況,給你有它設置,你可以做這樣的事情的方式:

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 
} 

如果我這樣做,我可能會做對自己有點更容易使用的自定義屬性。這將避免使用正則表達式或任何字符串操作,並會使代碼更簡單。

在您的「點擊」錨,給他們某種指標屬性的,像這樣:

<a class="clickable" id="itemid-01" href="" data-index="1"></a> 

然後你的js可以簡單地是:

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'); 

} 

如果你不希望它爲可讀和所有你需要的是一張ID:

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

這裏有一個fiddle

+0

嗨馬特,感謝您的解釋,自定義屬性是一個很好的接觸。我給了這個嘗試,但只能讓它獲得一個鏈接。我需要它按順序遍歷所有鏈接。有點像下一個按鈕可以在燈箱中做一個更好的解釋。 –

+0

讓我確定我是理解:那麼,我提供的工作方式是你所期待的嗎?我的理解是,只要點擊給定的錨點,就需要根據序列獲取下一個(也是唯一的下一個)ID。小提琴以這種方式工作。也許我沒有完全明白你在找什麼?或者如果你想發佈你的代碼,我可以看看! –