JS图片预览问题,导致后续函数不执行问题?

以下是代码片段

使用的JS本地预览图片,上传后加载到预先写好的DIV中,然后图片加载完成后后续函数弹出的提示 很快背覆盖

甚至其他功能弹出的提示也并不能使用,困惑很久,求解答。


function PreviewImage(divImage, upload) {

    if (checkPic()) {

        try {

            //console.log(upload.files[0]);

            var imgPath = getObjectURL(upload.files[0]);

            // $(".divImage").css("background","url(imgPath)")

            document.getElementById(divImage).innerHTML = "<img id='imgPreview' class='bgimg1' src='" + imgPath + "' width='" + '100%' + "' height='" + '100%' + "'/>";

            $(".menu").hide();

            $(".menuswitch1").removeClass('menuswitch1').addClass('menuswitch');

            var appElement = document.querySelector('[ng-controller=screenCtrl]');

            var $scope = angular.element(appElement).scope();

            $(".success_text").html("更改背景图片成功");

            $scope.$apply(function () {

                $scope.success_display = false;

            })

            setTimeout(function () {

                $scope.$apply(function () {

                    $scope.success_display = true;

                })

            }, 2000);

        } catch (e) {

            alert("无法预览图片");

        }

    }

}


子衿沉夜
浏览 413回答 2
2回答

GCT1015

把jQ的部分改了改,字符串的地方看的脑仁疼。。。Angular的部分没动。function PreviewImage(divImage, upload) {&nbsp; &nbsp; if (checkPic()) {&nbsp; &nbsp; &nbsp; &nbsp; var imgPath = getObjectURL(upload.files[0]);&nbsp; &nbsp; &nbsp; &nbsp; $('<img>', {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "class": 'bgimg1',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id: 'imgPreview',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; css: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: '100%',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; height: '100%'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }).appendTo('#divImage');&nbsp; &nbsp; &nbsp; &nbsp; $('.menu').hide();&nbsp; &nbsp; &nbsp; &nbsp; $('.menuswitch1')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toggleClass('menuswitch1')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toggleClass('menuswitch');&nbsp; &nbsp; &nbsp; &nbsp; var appElement = $('[ng-controller="screenCtrl"]')[0];&nbsp; &nbsp; &nbsp; &nbsp; var $scope = angular.element(appElement).scope();&nbsp; &nbsp; &nbsp; &nbsp; $('.success_text').html('更改背景图片成功');&nbsp; &nbsp; &nbsp; &nbsp; $scope.$apply(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.success_display = false;&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; setTimeout(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.$apply(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $scope.success_display = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; }, 2000);&nbsp; &nbsp; }}另外,下次尽量别try这么大块的代码吧。。。还不如多console点或者等报错引号建议以单引号为主
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript