jquery - 如何访问任何用户系统上的文件

标签 jquery html css

// these files are stored on my system on these path : C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/        
     var gifArr = ['source.gif',
                  'source1.gif',
                  'source2.gif',
                  'source3.gif',
                  'source4.gif',
                  'source5.gif',
                  'source6.gif',
                  'source7.gif',
                  'source9.gif',
                  'source10.gif',
                  'source11.gif',
                  'source12.gif',
                  'source13.gif'
                 ];

    // function to select the random gif andchange the gif after 5 seconds

        function changeGif(){
            setInterval(function(){
                $('.add-gif').css({'background-image': 'url(C:/Users/ABHISHEK/Desktop/Songify-master/Songify-master/assets/images/' + gifArr[Math.floor(Math.random() * gifArr.length)] + ')'}); 
            },
            5000);
        }

函数 changeGif() 将选择随机 gif 并在 5 秒后更改背景。该函数将在我的系统上运行,因为我在函数中定义了静态路径。 但我希望下载我的应用程序并运行代码的用户,然后路径将根据用户的系统设置。 那么如何根据用户的系统获取文件的路径呢?

最佳答案

您需要提供脚本文件中图像的相对路径

根据 W3

Partial URLs are interpreted relative to the source of the style sheet, not relative to the document

url 是相对于文件位置的,所以这应该对你有用:

url('../../images/image.jpg')

$('.add-gif').css('backgroundImage','url(images/'  + gifArr[Math.floor(Math.random() * gifArr.length)] ) );

关于jquery - 如何访问任何用户系统上的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45217716/

相关文章:

php - jQuery Ajax 发布到 php 没有捕获变量

javascript - 在输入字段内使用图标后,默认 'input is required/Please fill out this field' 消息不显示

javascript - Bootstrap affix() 插件在滚动内容时出现问题

html - 如何在CSS中为动画背景图像设置原始图像

css - Bootstrap 3.6 - 高度为 100% 的 Div

javascript - 通过不同尺寸的div显示X

jQuery datepicker onSelect 调用 .append() 不起作用

jquery - 无法关闭我的覆盖登录表单

html - 在 CSS 网格内垂直对齐列表项

php - CSS子菜单 anchor 悬停问题