jquery get attr() method and appendTo html



>我试图获取我的部分的 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'));
});

通过一些查找,我发现当前的section具有类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

js小提琴演示

最新更新