javascript - 单击两者时,下一个和上一个按钮不起作用

标签 javascript jquery html banner

我正在尝试使用 jquery.currentBanner 值增加和减少的横幅 slider ,当我单击按钮但横幅没有改变时。

脚本:

$(document).ready(function () {
    var currentBanner = 0;
    $("#prev").bind("click", function () {
        currentBanner -= 1;
        $("#banner_div img").eq(currentBanner).fadeIn("slow");
    });

    $("#next").bind("click",function () {
        currentBanner += 1;
        $("#banner_div img").eq(currentBanner).fadeIn("slow");
    });

    $("#banner_div img").eq(currentBanner).css("display", "block");
});

HTML:

<a href="#" id="next>next</a>
<a href="#" id="prev">prev</a>
<div id="banner_div">
    <img src="image1.jpg" alt="" />
    <img src="image2.jpg" alt="" />
    <img src="image3.jpg" alt="" />
    <img src="image4.jpg" alt="" />
</div>

最佳答案

马上行动

<a href="#" id="next>next</a>

缺少您的 id 属性的结束引号:

<a href="#" id="next">next</a>

Here's a jsFiddle demo of what I would do .

jQuery:

$(document).ready(function () {

    var $images = $("#banner_div img"),
        totalImages = $images.length,
        currentIndex = 0;

    $images.eq(currentIndex).fadeIn("slow");

    $("#prev").on("click", function() {
        if (currentIndex > 0) {
            $images.eq(currentIndex).stop(true,true).hide(0);
            currentIndex -= 1;
            $images.eq(currentIndex).stop(true,true).fadeIn("slow");
        }
    });

    $("#next").on("click",function () {
        if (currentIndex < totalImages-1) { 
            $images.eq(currentIndex).stop(true,true).hide(0);
            currentIndex += 1;
            $images.eq(currentIndex).stop(true,true).fadeIn("slow");
        }
    });

});​

CSS:

#banner_div img { display: none; }​

关于javascript - 单击两者时,下一个和上一个按钮不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10518945/

相关文章:

javascript - jeditable 意外触发嵌套项目上的 Draggable

javascript - 什么是好的 Javascript 教程库?

javascript - 在没有请求访问 token 的情况下检查用户是否喜欢 Facebook 页面

javascript - 原生 javascript 中 jQuery 函数的 after 和 before 等价于什么?

javascript - jQuery Div 向右移动但不向左移动时缩小

html - html中的字体错误

jquery - Scrollspy 与 ancor 过渡

javascript - 正则表达式替换括号中的键、值

javascript - 如何让按键触发链接

html - 如何正确使用 CSS 动画延迟来创建可折叠的 HTML 容器?