-1

我试图获取我的部分的 id 并将 id 属性附加到单击的按钮。

所以它会显示:

<div class="navigate-left">PREVIEW ID NAME SECTION</div>

<div class="navigate-right">NEXT ID NAME SECTION</div>

任何帮助将非常感激。

http://jsfiddle.net/3fJqZ/67/

$('.navigate-left').click(function() {
 $(this).attr('id').appendTo($('.navigate-left'));
});

$('.navigate-right').click(function() {
    $(this).attr('id').appendTo($('.navigate-right'));
});
4

1 回答 1

2

通过一些查看,我发现 currentsection有 class present。因此,您可以使用以下代码获取当前部分的 id。

$('.navigate-left').click(function () {
     var currentSection = $('section.present');
     var prevId = currentSection.prev('section').attr('id');
     var nextId = currentSection.next('section').attr('id');

     $(this).text(prevId);
     $('.navigate-right').text(nextId);

 });

 $('.navigate-right').click(function () {
     var currentSection = $('section.present');
     var prevId = currentSection.prev('section').attr('id');
     var nextId = currentSection.next('section').attr('id');

     $('.navigate-left').text(prevId);
     $(this).text(nextId);
 });

编辑:下面的演示在箭头下方为您提供了上一节和下一节的 ID

jsFiddle 演示

于 2013-10-17T14:22:44.953 回答