javascript - 无法在 jquery 上使用上一项和下一项链接

标签 javascript jquery html

我正在尝试循环浏览两个项目:上一个项目和下一个项目选项。它最初有效,但是当我循环到下一个项目时,我无法返回到上一个项目。知道为什么吗?

HTML

<section class="st" id="item1">
Item 1<p>
    <a id="prevItem" href="#">Previous Item</a>
    <a id="nextItem" href="#">Next Item</a>
</section>

<section class="st" id="item2">
Item 2
    <p>
    <a id="prevItem" href="#">Previous Item</a>
    <a id="nextItem" href="#">Next Item</a>
</section>

JS

$("#item2").hide()
$("#nextItem").click(function(){        
    if($('#item1').is(':visible')) {
        $("#item1").hide();
        $("#item2").show();
    }
    else if($('#item2').is(':visible')) {
        $("#item1").show();
        $("#item2").hide();
    }
});

这里是 Jsfiddle:http://jsfiddle.net/by0bze5e/

最佳答案

您不能拥有两个具有相同 id 的元素并期望它能够正常工作。将它们更改为类,它很可能会正常工作。

<a class="prevItem" href="#">Previous Item</a>
<a class="nextItem" href="#">Next Item</a>

$("#item2").hide()
$(".nextItem").click(function(){        
    $("#item1, #item2").toggle();
});

http://jsfiddle.net/by0bze5e/2/

关于javascript - 无法在 jquery 上使用上一项和下一项链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26636567/

相关文章:

javascript - 大 innerHTML 注入(inject)后访问 DOM

javascript - 将错误从一个异步函数传递到另一个

jquery - 使用 jQuery 从我的网站在 twitter/facebook 上发帖

javascript - jQuery:删除数据表中的行

javascript - 从 js 类分配事件监听器

javascript - impress.js - 元素符号列表

javascript - 添加 url 作为使用 Tesseract OCR 查询的参数

javascript - 如何让Jquery可排序插件使用表格中的多行进行拖动

html - 在边界半径图像背景上添加边界流出

html - 如何在更改颜色时不让我的边框移动?