2017-08-01 62 views
-1

对不起,与以前要求的版本不太一样。我尝试过这些解决方案无济于事。代码在JSfiddle中有效,但在我的网页上停止

我曾与一些人一起解决了一些我们正在处理的问题。最后让它在JSfiddle上工作,我们需要它,但无论出于何种原因,它不适用于纯HTML网站。该内容看起来加载,但预览后选择一个图像不起作用。

这里是的jsfiddle在这里工作:http://jsfiddle.net/ELcf6/568/

这里是所有编译成用于测试单个页面的代码片断。这是似乎不起作用的例子。 JSfiddle是否注入了我可能缺少的任何先决条件?

任何帮助搞清楚这将不胜感激。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<!DOCTYPE html> 
 
    <html> 
 
    <head> 
 
    <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
    <script> 
 
     var imageLoader = document.getElementById('filePhoto'); 
 
     imageLoader.addEventListener('change', handleImage, false); 
 
    
 
    function handleImage(e) { 
 
    \t \t var filetype = imageLoader.files[0].type; 
 
     var reader = new FileReader(); 
 
     reader.onload = function (event) { 
 
      if(filetype.indexOf("image") > -1){ 
 
      $('.uploader img').attr('src',event.target.result); 
 
      }else if(filetype.indexOf("video") > -1){ 
 
      
 
      $('.uploader video')[0].src =reader.result; 
 
      } 
 
      
 
     } 
 
     reader.readAsDataURL(e.target.files[0]); 
 
    } 
 
    </script> 
 
    <style type="text/css"> 
 
    .uploader { 
 
     position:relative; 
 
     overflow:hidden; 
 
     width:300px; 
 
     height:350px; 
 
     background:#f3f3f3; 
 
     border:2px dashed #e8e8e8; 
 
    } 
 
    
 
    #filePhoto{ 
 
     position:absolute; 
 
     width:300px; 
 
     height:400px; 
 
     top:-50px; 
 
     left:0; 
 
     z-index:2; 
 
     opacity:0; 
 
     cursor:pointer; 
 
    } 
 
    
 
    .uploader img,.uploader video{ 
 
     position:absolute; 
 
     width:302px; 
 
     height:352px; 
 
     top:-1px; 
 
     left:-1px; 
 
     z-index:1; 
 
     border:none; 
 
     object-fit:cover; 
 
    } 
 
    </style> 
 
    <title></title> 
 
    </head> 
 
    
 
    <body> 
 
     <div class="uploader" onclick="$('#filePhoto').click()"> 
 
     click here or drag here your images for preview and set userprofile_picture data 
 
     <img src=""/> 
 
     <video></video> 
 
     <input type="file" name="userprofile_picture" id="filePhoto" /> 
 
     </div> 
 
    </body> 
 
    </html>

+0

你在网站上的控制台中看到什么错误?请注意,您的代码也被包装在jsFiddle的onload处理程序中。你在网站上做的是一样的吗? – j08691

+0

唯一的错误与未知变量有关。像斯科特建议的那样将脚本移到页面下面解决了这个问题。 – fbords

+0

这是解决问题的一种方法。另一个是将代码封装在document.ready或window.load调用中,就像jQuery默认的那样。你只是没有看到这是默认的。将代码加载到页面的末尾是jsFiddle中的另一个选项。 – j08691

回答

0

的DOM加载之前你的脚本运行,这就是为什么你所得到的“无法读取空的特性‘的addEventListener’”的错误。

移动脚本,使其位于body标记关闭之前。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
    <!DOCTYPE html> 
 
     <html> 
 
     <head> 
 
     <meta http-equiv="content-type" content="text/html; charset=UTF-8"> 
 

 
     <style type="text/css"> 
 
     .uploader { 
 
      position:relative; 
 
      overflow:hidden; 
 
      width:300px; 
 
      height:350px; 
 
      background:#f3f3f3; 
 
      border:2px dashed #e8e8e8; 
 
     } 
 
     
 
     #filePhoto{ 
 
      position:absolute; 
 
      width:300px; 
 
      height:400px; 
 
      top:-50px; 
 
      left:0; 
 
      z-index:2; 
 
      opacity:0; 
 
      cursor:pointer; 
 
     } 
 
     
 
     .uploader img,.uploader video{ 
 
      position:absolute; 
 
      width:302px; 
 
      height:352px; 
 
      top:-1px; 
 
      left:-1px; 
 
      z-index:1; 
 
      border:none; 
 
      object-fit:cover; 
 
     } 
 
     </style> 
 
     <title></title> 
 
     </head> 
 
     
 
     <body> 
 
      <div class="uploader" onclick="$('#filePhoto').click()"> 
 
      click here or drag here your images for preview and set userprofile_picture data 
 
      <img src=""/> 
 
      <video></video> 
 
      <input type="file" name="userprofile_picture" id="filePhoto" /> 
 
      </div> 
 
      
 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
 
     <script> 
 
      var imageLoader = document.getElementById('filePhoto'); 
 
      imageLoader.addEventListener('change', handleImage, false); 
 
     
 
     function handleImage(e) { 
 
     \t \t var filetype = imageLoader.files[0].type; 
 
      var reader = new FileReader(); 
 
      reader.onload = function (event) { 
 
       if(filetype.indexOf("image") > -1){ 
 
       $('.uploader img').attr('src',event.target.result); 
 
       }else if(filetype.indexOf("video") > -1){ 
 
       
 
       $('.uploader video')[0].src =reader.result; 
 
       } 
 
       
 
      } 
 
      reader.readAsDataURL(e.target.files[0]); 
 
     } 
 
     </script> 
 
      
 
     </body> 
 
     </html>

+0

谢谢Scott。我有一种感觉,就是这样的。 我不认为你会有任何想法如何编辑上面的脚本有视频播放吗?该脚本是用于图像和视频的拖放预览器。它这样做,但它只会显示视频的第一帧。我想要它看起来似乎正在展示一个样本。或者短短几秒钟,显示部分视频的动画缩略图或其他方法将其与图像区分开来。再次感谢 – fbords

+0

@fbords查看[this](https://developer.mozilla.org/en-US/Apps/Fundamentals/Audio_and_video_delivery)了解详情。 –

相关问题