【问题标题】:Help me change my jquery code to add additional div帮我更改我的 jquery 代码以添加额外的 div
【发布时间】:2011-08-26 06:02:24
【问题描述】:

我有一个幻灯片,它只是改变照片。我需要在每张照片的顶部添加一个新的 div 框。目前jquery 代码只需要#slideshow 和.active。如何修改它以获取新的 div 类“.newbox”(第一张照片为 .newbox1,第二张照片为 .newbox2,依此类推)。每次不同的盒子必须出现在不同的照片之上。这是我的照片幻灯片 jquery 代码:

function slideSwitch() {
    var $active = $('#slideshow DIV.active');

    if ( $active.length == 0 ) $active = $('#slideshow DIV:last');

    // use this to pull the divs in the order they appear in the markup
    var $next =  $active.next().length ? $active.next()
        : $('#slideshow DIV:first');

    // uncomment below to pull the divs randomly
    // var $sibs  = $active.siblings();
    // var rndNum = Math.floor(Math.random() * $sibs.length );
    // var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active');

    $next.css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
}

$(function() {
    setInterval( "slideSwitch()", 5000 );
});

</script>

我的html:

div id="slideshow">
   <div class="active">
        <a href="http://www.mylink.com/" target="_blank"><img src="http://route/img.jpg" alt="alt" /></a>

    <div class="newbox"></div>
    </div>

【问题讨论】:

    标签: jquery


    【解决方案1】:

    你应该使用 jquerys wrap 方法

    <div class="container">
      <div class="inner">Hello</div>
      <div class="inner">Goodbye</div>
    </div>
    

    要包装的 jquery 代码

    $('.inner').wrap('<div class="new" />');
    

    变成

    <div class="container">
      <div class="new">
        <div class="inner">Hello</div>
      </div>
      <div class="new">
        <div class="inner">Goodbye</div>
      </div>
    </div>
    

    在你的情况下,我假设它会是

    $("img"). wrap('<div class="new" />');
    

    【讨论】:

      猜你喜欢
      • 2015-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 2017-07-06
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多