增加DIV数字变量的按钮

Button to increase an DIV number variable

本文关键字:按钮 变量 数字 DIV 增加      更新时间:2023-09-26

我有以下8张图表

https://jsfiddle.net/BernydotJAr/grwzfjsn/

当然,每个都有不同的DIV ID,我试着在中间放一个按钮,像幻灯片一样在它们之间切换或更改,我可以更改图像,但不能更改DIV。

<div id="chartdiv"></div>               
<div id="chartdiv2"></div>          

我尝试过Jquery,但我无法显示div中的内容。所以当我单击按钮时,它应该添加一个数字,比如chartdiv&1、chartdiv&2等等,这样我就可以一个接一个地显示。下面是jquery代码(我是从这里得到的:http://www.html5marketplace.com/slideshow/direct/index.html)

<script>
$(window).load(function(){
    var pages = $('#container li'), current=0;
    var currentPage,nextPage;
    var handler=function(){
        $('#container .button').unbind('click');
        currentPage= pages.eq(current);
        if($(this).hasClass('prevButton'))
        {
            if (current <= 0)
                current=pages.length-1;
            else
                current=current-1;
            nextPage = pages.eq(current);   
            nextPage.css("marginLeft",-604);
            nextPage.show();
            nextPage.animate({ marginLeft: 0 }, 800,function(){
                currentPage.hide();
            });
            currentPage.animate({ marginLeft: 604 }, 800,function(){
                $('#container .button').bind('click',handler);
            });
        }
        else
        {
            if (current >= pages.length-1)
                current=0;
            else
                current=current+1;
            nextPage = pages.eq(current);   
            nextPage.css("marginLeft",604);
            nextPage.show();
            nextPage.animate({ marginLeft: 0 }, 800,function(){
            });
            currentPage.animate({ marginLeft: -604 }, 800,function(){
                currentPage.hide();
                $('#container .button').bind('click',handler);
            });
        }
    }
    $('#container .button').click(handler);
   });
</script>

根据您提供的代码和从其他站点获得的代码,div必须位于li元素内部。像这样的

<div id="container">
<ul>
<li>
  <div id="chartdiv"></div>         
</li>
<li style="display:none;">
  <div id="chartdiv2"></div>            
</li>
<li style="display:none;">
  <div id="chartdiv3"></div>            
</li>
</ul>
<span class="button prevButton"></span>
<span class="button nextButton"></span>
</div>

请参阅此处的完整代码

https://jsfiddle.net/grwzfjsn/3/