javascript - 进行奇特的文件输入时未捕获类型错误

标签 javascript php jquery wordpress typeerror

我正在使用我在网上找到的一些代码,这将为我提供一个精美的文件上传按钮,该按钮还允许用户将文件拖放到放置区域中。它在 jsfiddle 中工作得很好但是当我通过 Cpanel 将代码添加到我的 Wordpress 文件时,它不起作用。我什么也没做,只是复制并粘贴文件。

错误:

未捕获类型错误:未定义不是函数 指向这个:$(window).load(function(){

未捕获类型错误:$container.imagesLoaded 不是函数 指出 5 件事:

  1. $container.imagesLoaded(function(){
  2. m.Callbacks.j @ jquery.js?ver=1.11.3:2
  3. m.Callbacks.k.fireWith @ jquery.js?ver=1.11.3:2
  4. m.extend.ready @ jquery.js?ver=1.11.3:2
  5. J@jquery.js?ver=1.11.3:2

functions.php

<?php

function my_scripts() {
wp_enqueue_script( 'bootstrap-js', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js', array('jquery'), '3.3.5', true );
wp_enqueue_script( 'custom-js', get_stylesheet_directory_uri() . '/js/custom-js.js');
}
add_action('wp_enqueue_scripts', 'my_scripts');

function my_styles() {
wp_enqueue_style( 'bootstrap-css', '//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css' );
wp_enqueue_style( 'my-style', get_stylesheet_directory_uri() . '/style.css');
}
add_action('wp_enqueue_scripts', 'my_styles');

?>

自定义js.js

$(window).load(function(){
    $('#drop').click(function(){
        console.log('click');
        $('#fileBox').trigger('click');
    });
    //Remove item
    $('.fileCont span').click(function(){
        $(this).remove();
    });
});
if (window.FileReader) {
    var drop;
    addEventHandler(window, 'load', function () {
        var status = document.getElementById('status');
        drop = document.getElementById('drop');
        var list = document.getElementById('list');

        function cancel(e) {
            if (e.preventDefault) {
                e.preventDefault();
            }
            return false;
        }

        // Tells the browser that we *can* drop on this target
        addEventHandler(drop, 'dragover', cancel);
        addEventHandler(drop, 'dragenter', cancel);

        addEventHandler(drop, 'drop', function (e) {
            e = e || window.event; // get window.event if e argument missing (in IE)   
            if (e.preventDefault) {
                e.preventDefault();
            } // stops the browser from redirecting off to the image.

            var dt = e.dataTransfer;
            var files = dt.files;
            for (var i = 0; i < files.length; i++) {
                var file = files[i];
                var reader = new FileReader();

                //attach event handlers here...

                reader.readAsDataURL(file);
                addEventHandler(reader, 'loadend', function (e, file) {
                    var bin = this.result;
                    var fileCont = document.createElement('div');
                    fileCont.className = "fileCont";
                    list.appendChild(fileCont);

                    var fileNumber = list.getElementsByTagName('img').length + 1;
                    status.innerHTML = fileNumber < files.length ? 'Loaded 100% of file ' + fileNumber + ' of ' + files.length + '...' : 'Done loading. processed ' + fileNumber + ' files.';

                    var img = document.createElement("img");
                    img.file = file;
                    img.src = bin;
                    img.className = "thumb";
                    fileCont.appendChild(img);

                    var newFile = document.createElement('div');
                    newFile.innerHTML = file.name;
                    newFile.className = "fileName";
                    fileCont.appendChild(newFile);

                    var fileSize = document.createElement('div');
                    fileSize.className = "fileSize";
                    fileSize.innerHTML = Math.round(file.size/1024) + ' KB';
                    fileCont.appendChild(fileSize);

                    var progress = document.createElement('div');
                    progress.className = "progress";
                    progress.innerHTML = '<div aria-valuemax="100" aria-valuemin="0" aria-valuenow="100" class="progress-bar progress-bar-success" role="progressbar" style="width: 100%"></div>';
                    fileCont.appendChild(progress);

                    var remove = document.createElement('div');
                    remove.className = "remove";
                    remove.innerHTML = '<span class="glyphicon glyphicon-remove"></div>';
                    list.appendChild(remove);


                }.bindToEventHandler(file));
            }
            return false;
        });
        Function.prototype.bindToEventHandler = function bindToEventHandler() {
            var handler = this;
            var boundParameters = Array.prototype.slice.call(arguments);
            //create closure
            return function (e) {
                e = e || window.event; // get window.event if e argument missing (in IE)   
                boundParameters.unshift(e);
                handler.apply(this, boundParameters);
            }
        };
    });
} else {
    document.getElementById('status').innerHTML = 'Your browser does not support the HTML5 FileReader.';
}


function addEventHandler(obj, evt, handler) {
    if (obj.addEventListener) {
        // W3C method
        obj.addEventListener(evt, handler, false);
    } else if (obj.attachEvent) {
        // IE method.
        obj.attachEvent('on' + evt, handler);
    } else {
        // Old school method.
        obj['on' + evt] = handler;
    }
}


//Not plugged yet
var bar = $('.progress-bar');
$(function(){
  $(bar).each(function(){
    bar_width = $(this).attr('aria-valuenow');
    $(this).width(bar_width + '%');
  });
});

子主题的style.css

body{
    color:#666;
}
#drop {
    border: 1px dashed #ccc;
    width: 450px;
    min-height: 35px;
    margin: 10px auto;
    -webkit-border-radius: 4px;
    -moz-border-radius: 4px;
    border-radius: 4px;
    display:box;
    cursor:pointer;
}
#status {
    width:450px;
    margin: 0 auto;
}
#list {
    width:450px;
    margin: 0 auto;
}
.msg-drop{
    padding:10px;
}
.thumb {
    width:33px;
    height:33px;
    float:left;
    margin:3.5px;
    border: 1px solid #ccc;
}
.fileCont{
    display:block;
    width:425px;
    height:40px;
    margin:2.5px;
    float:left;
}
.fileSize{
   text-align:right;
    margin:2.5px;
    font-size:12px;
    padding-right: 10px;
}
.fileName{
    float:left;
    padding:2.5px;
    font-weight:700;
    text-transform: capitalize;    
}
.remove{
    width:20px;
    height:40px;
    padding-top: 13px;
    font-size: 18px;
    float:left;
}
.progress {
  height: 6px!important;
  width: 380px;
  margin-top: 5px;
}
.progress-bar {
  -webkit-transition: width 1.5s ease-in-out;
  transition: width 1.5s ease-in-out;
}
#fileBox{
    display:none;
}

最佳答案

您是否包含 jquery 库?如果您还没有添加,请添加

if (!is_admin()) add_action("wp_enqueue_scripts", "my_jquery_enqueue", 11); 函数 my_jquery_enqueue() { wp_deregister_script('jquery'); wp_register_script('jquery', "http". ($_SERVER['SERVER_PORT'] == 443 ? "s": "") . ":https://ajax.googleapis.com/ajax/libs/angularjs/1.3. 15/angular.min.js", false, null); wp_enqueue_script('jquery'); }

在functions.php文件中

如果您已经这样做了,请检查 var container 是否已在任何位置设置。我在您的示例中没有看到该变量的定义

关于javascript - 进行奇特的文件输入时未捕获类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160060/

相关文章:

javascript - 使用过滤器在 Angularjs 中执行搜索后清除列表?

php - 按元数据的值对用户进行排序

jquery - 显示div的iframe onclick

javascript - 如何在 phonegap 中同时使用相机 API 选择(选择)多张图像?

javascript - 数字数组可以转化为函数数组吗?

php - 如何将 JavaScript 函数数据放入 PHP 变量中

php - 当php mysql中删除类别及其子类别时,如何更新帖子类别关系?

jQuery 上一个下一个元素

jquery动画到某个位置

javascript - JSON.parse(JSON.stringify(x)) 目的?