好的,所以看图像,突出显示的部分是我需要得到的元素。我需要存储上面示例中的2391的data-id值。
因此,从数据标识值为" 2392"的锚标记开始,我该如何实现这一点。
这就是我所做的一切,没有快乐。
// This is finding the element, currentID is 2392.
var currentReview = $('.modelLink[data-id="'+ currentID +'"]');
var nextID = currentReview.closest('tr').next().children('td').next().next().next().children('.modalLink').data('id');
我怎样才能实现这个目标?
答案 0 :(得分:2)
尝试
var currentReview = $('.modelLink[data-id='+ currentID +']');
var nextID = currentReview.closest('tr').next().find('td a.modalLink').data('id');
答案 1 :(得分:0)
不是试图从一个深度嵌套的锚点遍历到另一个,而是将所有ID集中到数组中并遍历数组 - 更容易!
// Put all anchor IDs into an array
var ids = [];
$.each("a.modelLink", function( index, value ) {
ids.push(value.attr("data-id"));
});
// Creates: ids[0] = 2391, ids[1] = 2392, etc.
之后,您将能够遍历锚点而无需担心DOM遍历。
// This is finding the element, currentID is 2392.
var currentID = 2392;
var currentReview = $('.modelLink[data-id="'+ currentID +'"]');
// Ensure we do not go out of bounds
current_index = ids.indexOf(currentID);
if(current_index >= 0 && current_index < ids.length - 1) {
nextID = ids[current_index + 1];
}
else {
// Start over from the beginning
nextID = ids[0];
}
// Easily assign the nextReview
nextReview = $('.modelLink[data-id="'+ nextID +'"]');