dongyi1921 2014-04-28 17:11
浏览 404
已采纳

如何使用jquery更改动态内容的类名

In my php file I have this code section to get all jpg files form a folder and put them in to img tags.

if ($handle = opendir($dir)) {
    while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'jpg') {
           echo '<div class="image_slider"><img src ="' . $dir . $file . '"/></div>';
       }
    }
    closedir($handle);
}

and then I want to change displaying image by clicking on a links call previous and next.

To do so I'm using jQuery this way..

To show first image without clicking any thing

 $( ".image_slider img" ).first().addClass( "active" );

and then for the other images to load user must clik prvs or next links.

function next_img(){
        $( ".image_slider img" ).closest().next( "active" );
    }

but this function is not working. It doesn't change the class "active" from first one to second one.

I feel I'm missing something. but couldn't find it.

Thank you

  • 写回答

4条回答 默认 最新

  • duanduan8439 2014-04-28 18:22
    关注

    i want to enhance your code to follow

    $s='';
    $first=false;
    $handle=@opendir($dir);
    
    if ($handle) {
        $s='<div class="image_slider">';
    
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && strtolower(substr($file, strrpos($file, '.') + 1)) == 'jpg') {
    
                $cls='';
                if (!$first) {$cls='active'; $first=true;}
    
                $s.='<img class="'.$cls.'" src ="' . $dir . $file . '"/>';
    
            }
        }
    
        $s.='</div>';
        closedir($handle);    
    }
    else {
        echo 'Error in reading image folder-or the dir doesnt exist!';
    }
    

    and about slider: you can use the the active to get the current active and then use .next() to get the next element.

    i wrote a jsfiddle for you, hope you find it useful a sample jsfiddle about using .next()

    the function i wrote is :

    function activeNext(){
        var act=$('div.active');
        var img=$('.img_slider');
        if (act.is(img.last())) return false;
    
        act.removeClass('active').next().addClass('active');  
    }
    
    function activePrev(){
        var act=$('div.active');
        var img=$('.img_slider');
        if (act.is(img.first())) return false;
    
        act.removeClass('active').prev().addClass('active');  
    }
    

    just a brief explanation:

    1- get the current active and save it in act

    2- get all the image and store in img

    3- if current active one is first or last one then do nothing

    4- remove current active one. find prev/next item and make it active

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥20 为什么我写出来的绘图程序是这样的,有没有lao哥改一下
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥200 关于#c++#的问题,请各位专家解答!网站的邀请码
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号