码迷,mamicode.com
首页 > Windows程序 > 详细

一个按钮触发多个a标签,只有一个可以下载,其他的window.open()被浏览器拦截

时间:2018-05-17 19:56:19      阅读:212      评论:0      收藏:0      [点我收藏+]

标签:sele   read   open   charset   reader   delay   on()   var   reac   

点击一个标签,触发多个a标签的下载事件,会出现只有一个文件下载,其他都被拦截,

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
    <button id=‘download‘>下载</button>
    <!-- <input type="text" name=‘filePath‘> -->
</body>
<script>
    // window.onload = function(){
        $(function(){
            $("#download").click( function(){//点击下载按钮
        alert(‘下载‘)
            let triggerDelay = 100;
            let removeDelay = 1000;
            let url_arr=[‘http://download.huduntech.com/software/smartpdf/installer/xunjiepdfreader.exe‘,‘http://download.huduntech.com/software/smartpdf/installer/xunjiepdfreader.exe‘,‘http://download.huduntech.com/software/smartpdf/installer/xunjiepdfreader.exe‘,‘http://download.huduntech.com/software/smartpdf/installer/xunjiepdfreader.exe‘];
            //多个file文件选择checkbox
            // $(‘input[name="filePath"]:checked‘).each(function(){
            //     url_arr.push($(this).val());//取到下载url
            // });
            url_arr.forEach(function(item,index){
                _createIFrame(item, index * triggerDelay, removeDelay);
                console.log(‘index‘, index)
            })
            function _createIFrame(url, triggerDelay, removeDelay) {
                //动态添加iframe,设置src,然后删除
                setTimeout(function() {
                    var frame = $(‘<iframe style="display: none;" class="multi-download"></iframe>‘);
                    frame.attr(‘src‘, url);
                    $(document.body).after(frame);
                    setTimeout(function() {
                        frame.remove();
                    }, removeDelay);
                }, triggerDelay);
            }
        })
       
        })
     

    // }
</script>
</html>
react中用原生的js去写
这是基于antd的react
 openDown () {
        const triggerDelay = 100;
        const removeDelay = 1000;
        const {createIFrame}= this
        this.state.selectedRows.forEach((item, index)=>{
            createIFrame(item.fileUrl, index * triggerDelay, removeDelay)
        })

    },
    createIFrame (url, triggerDelay, removeDelay) {
        setTimeout(function (){
            // 动态添加iframe,设置src,然后删除
            const frame = document.createElement(‘iframe‘) //创建a对象
            frame.setAttribute(‘style‘, ‘display: none‘)
            frame.setAttribute(‘src‘, url)
            frame.setAttribute(‘id‘, ‘iframeName‘)
            document.body.appendChild(frame)
            setTimeout(function(){
                const node =  document.getElementById(‘iframeName‘)
                node.parentNode.removeChild(node)
            }, removeDelay)
        }, triggerDelay)
    },

  

  

一个按钮触发多个a标签,只有一个可以下载,其他的window.open()被浏览器拦截

标签:sele   read   open   charset   reader   delay   on()   var   reac   

原文地址:https://www.cnblogs.com/summerXll/p/9052681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!