dongxi5494 2014-11-07 18:37
浏览 51
已采纳

如何在onclick函数中使用javascript淡出多个元素

SOF users: today I have another challenge for you:

I have this piece of code, a part of jquery Uploader. Each file uploaded uses this code, where I create correct answer if file was uploaded ok or not.

When a file is uploaded ok, I put a delete button (cannot use form tag here). I want to add a fadeout effect.

EDIT: now all the interesting code.

<script>
    var conexion;
    var numarch = 0;
    var idactual = 0;
    function crearXMLHttpRequest(){
        var xmlHttp=null;
        if (window.ActiveXObject) 
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        else
            if (window.XMLHttpRequest) 
                xmlHttp = new XMLHttpRequest();
        return xmlHttp;
    }

    function borrar_archivo(iddiv, ruta, header){
        conexion=crearXMLHttpRequest();
        idactual = iddiv;
        conexion.onreadystatechange = procesarEventos;
        conexion.open(header, ruta, true);
        conexion.send(null);
    }

    function procesarEventos(){
        var detalles = document.getElementById(idactual);
        if(conexion.readyState == 4){
            var resultado = conexion.responseText;
            if(resultado.indexOf("true")!=-1){
                nomArchivo = conexion.responseText.split(":",1);
                nombreArchivo = nomArchivo[0].substring(1);
                detalles.innerHTML = "<p style='float: left; clear:left; color: #66CC00; font-size:12px;'>"+nombreArchivo+" ha sido borrado.";
            }
            else{
                detalles.innerHTML = "<p style='float: left; clear:left; font-size:12px;' class='text-danger'>Ha habido un error al borrar el archivo.</p>";
            }
        }
        else{
            detalles.innerHTML = "<p style='float: left; clear:left; font-size:12px;'>Cargando...</p>";
        }
        setInterval(function(){
             $("#"+idactual).fadeOut(1000);
        },1500);
    }

    $(function(){
        $('#fileupload').fileupload({
            dataType: 'json',
            done: function (e, data){
                $.each(data.result.files, function (index, file) {
                    numarch++;
                    $('<div id="archivo_'+numarch+'" />').appendTo('#files');
                    if (file.error){
                        $('<img style="width: 16px; float: left; clear:left; margin-right: 10px;" src="img/x.png" title="Error" alt="Error"/>').appendTo('#archivo_'+numarch);
                        $('<p style="float: left; font-size:12px;" class="text-danger"/>').text(file.error).appendTo('#archivo_'+numarch);
                    }
                    else{
                        var newFileDiv = $("<img style='width: 16px; float: left;  clear:left; margin-right: 10px;' src='img/v.png' title='Archivo subido OK' alt='Archivo subido OK'/><p style='float: left; color: #66CC00; font-size:12px;'>"+file.name+"</p><div style='float :left; height: 5px;margin-left: 25px;' class='btn btn-danger delete' onclick=borrar_archivo('archivo_"+numarch+"','"+file.deleteUrl+"','"+file.deleteType+"')><i style='top: -5px;left: -5px;' class='glyphicon glyphicon-trash'></i><span style='top: -6px;position: relative;'>Borrar</span></div>");
                        $('#archivo_'+numarch).append(newFileDiv);
                    }
                });
            },
            progressall: function (e, data){
                var progress = parseInt(data.loaded / data.total * 100, 10);
                $('#progress .progress-bar').html(progress + '%');
                $('#progress .progress-bar').css('width',progress + '%');
            }
        });
    });
</script>

I just founded that I can "delete" one item ok. but when I use multiple buttons to delete a lot of items, the function breaks, only deleting last elements I clicked...

I want to do this fadeout effect asynchronous.(that is the reason for the setInterval instead setTimeout)...

But nothing works for me. Now I am little lost about this.

Any help, please?

EDIT 2:

Now, trying I find how to delete 2 or more items, but the fadeout() effect only works on first one:

function borrar_archivo(iddiv, ruta, header){
    conexion=crearXMLHttpRequest();
    idactual = iddiv;
    conexion.onreadystatechange = procesarEventos;
    conexion.open(header, ruta, true);
    conexion.send(null);
    setInterval(function(){
        $("#"+idactual).fadeOut(1000);
    },1500);
    setInterval(function(){
        $("#"+idactual).remove();
    },1000);
}

Any idea why this happend? and how to solve it?

  • 写回答

2条回答 默认 最新

  • dongmi19720408 2014-11-17 18:27
    关注

    Ok, until a lot of research, I founded the solution:

    When you have a bunch of elements with a Delete button, all buttons calls to the same function: this is a problem when you work with timers, because when you are in "the waiting time" and you re-execute that timers, the inner action of the timer can be stopped(overwrited before has been sended).

    The solution I found is this: put the wait time lesser than the fadeout time, not allowing the inner function to be stopped. Why this works? I don't know, but you can overclick the buttons and all the inner functions work.

    I paste here my code. If you need more code to understand it, write it: idactual is the id name of the div to be faded out. numarc is the number used to identify each div: (e.g.: idactual = archivo_1 , numarch = 1)

    setInterval(function(){
        $("#"+idactual).fadeOut(1500, function(){
            var i=0;
            for(i=1;i>numarc;i++){
                elemento=$(document).getElementById("archivo_"+i);
                texto = elemento.innerHTML;
                if(texto.indexOf("borrado")!=-1){
                    elemento.remove();
                }
            }
        });
    },100);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)