JS历理 优化foot.js脚本

document.writeln("<div id=\'outerdiv\' style=\'position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;\'>");
document.writeln("<div id=\'innerdiv\' style=\'position:absolute;\'>");
document.writeln("<img id=\'bigimg\' style=\'border:5px solid #fff;\' src=\'\' /></div></div>");
$(function(){
    //图片放大的方法
    $(".pimg").click(function(){
        var _this = $(this);
        imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
    });

    //列表的展开与收缩的方法
    $('#opener').click(function() {
        $('.ui-content').addClass('layui-show');
    });
    $('#toggle').click(function() {
        $('.ui-content').toggleClass('layui-show');
    });

    //多行文本域的高度为其内容的高度
    $.fn.autoHeight = function(){
        function autoHeight(elem){
            elem.style.height = 'auto';
            elem.scrollTop = 0;
            elem.style.height = elem.scrollHeight + 'px';
        }
        this.each(function(){
            autoHeight(this);
            $(this).on('keyup', function(){
                autoHeight(this);
            });
        });
    }
    $('textarea[autoHeight]').autoHeight();


});

//开启katex数学模式
document.addEventListener("DOMContentLoaded", function() {
    // 指定渲染容器
    const mathContainers = document.querySelectorAll(".katex");
    // 遍历所有容器
    mathContainers.forEach(container => {
        renderMathInElement(container, {
            delimiters: [
                {left: '```math', right: '```', display: true},
                {left: '`$', right: '$`', display: false}
            ],
            throwOnError: false
        });
    });
});

//图片放大的方法
function imgShow(outerdiv, innerdiv, bigimg, _this){
    var src = _this.attr("src");
    $(bigimg).attr("src", src);
    $("<img/>").attr("src", src).load(function(){
        var windowW = $(window).width();
        var windowH = $(window).height();
        var realWidth = this.width;
        var realHeight = this.height;
        var imgWidth, imgHeight;
        var scale = 0.9;
        if(realHeight>windowH*scale) {
            imgHeight = windowH*scale;
            imgWidth = imgHeight/realHeight*realWidth;
            if(imgWidth>windowW*scale) {
                imgWidth = windowW*scale;
            }
        } else if(realWidth>windowW*scale) {
            imgWidth = windowW*scale;
            imgHeight = imgWidth/realWidth*realHeight;
        } else {
            imgWidth = realWidth;
            imgHeight = realHeight;
        }
        $(bigimg).css("width",imgWidth);
        var w = (windowW-imgWidth)/2-5;
        var h = (windowH-imgHeight)/2-5;
        $(innerdiv).css({"top":h, "left":w});
        $(outerdiv).fadeIn("fast");
    });

    $(outerdiv).click(function(){
        $(this).fadeOut("fast");
    });
}

//删除数据的方法
function setDelete(v,n){
    var url = window.location.href;
    var path = encodeURIComponent(url); //URL需要编码传输
    var value = window.atob(decodeURIComponent(v));
    var name = window.atob(decodeURIComponent(n));
    layer.confirm('你确定要删除'+name+'-'+value+'的这条信息吗', {
        btn: ['确定','取消']
    }, function(){
        location.href="/Z/ZZZ/setDelete.php?f="+v+"&t="+n+"&p="+path;
    }, function(){
        layer.msg('已取消操作!');
    });
}

//临时标识恢复默认值
function setFlag(v,n){
    var url = window.location.href;
    var path = encodeURIComponent(url); //URL需要编码传输
    var value = window.atob(decodeURIComponent(v));
    var name = window.atob(decodeURIComponent(n));
    layer.confirm('你确定要'+name+'的临时标识全修改为'+value+'吗', {
        btn: ['确定','取消']
    }, function(){
        location.href="/Z/ZZZ/setFlag.php?f="+v+"&t="+n+"&p="+path;
    }, function(){
        layer.msg('已取消操作!');
    });
}

//临时序号恢复默认值
function setSort(v,n){
    var url = window.location.href;
    var path = encodeURIComponent(url); //URL需要编码传输
    var value = window.atob(decodeURIComponent(v));
    var name = window.atob(decodeURIComponent(n));
    layer.confirm('你确定要'+name+'的临时序号全修改为'+value+'吗', {
        btn: ['确定','取消']
    }, function(){
        location.href="/Z/ZZZ/setSort.php?f="+v+"&t="+n+"&p="+path;
    }, function(){
        layer.msg('已取消操作!');
    });
}

//自定义处理
function setTable(str){
    var url = window.location.href;
    var path = encodeURIComponent(url);
    layer.confirm(decodeURIComponent(str)+'<br>你确定要修改吗', {
        btn: ['确定','取消']
    }, function(){
        location.href="/Z/ZZZ/setTable.php?f="+str+"&p="+path;
    }, function(){
        layer.msg('已取消操作!');
    });
}

//下拉菜单二级联动
layui.use('form', function(){
    var form = layui.form;
    form.on('select(cbig_idFilter)', function(data){
        var id = data.value;
        var tb = $(data.elem).data('tb');
        var mid = $(data.elem).data('mid');
        if(id!=0){
            $("#cmin_id").empty();
            form.render('select');
            $.post("/Z/ZZZ/filterCbig_id.php", { id: id,tb: tb,mid: mid }, function(data){
                $("#cmin_id").html(data);
                form.render('select');
            });
        }
    });
    form.on('select(c2big_idFilter)', function(data){
        var id = data.value;
        var tb = $(data.elem).data('tb');
        var mid = $(data.elem).data('mid');
        if(id!=0){
            $("#c2min_id").empty();
            form.render('select');
            $.post("/Z/ZZZ/filterC2big_id.php", { id: id,tb: tb,mid: mid }, function(data){
                $("#c2min_id").html(data);
                form.render('select');
            });
        }
    });
    form.on('select(unitbig_idFilter)', function(data){
        var id = data.value;
        var tb = $(data.elem).data('tb');
        var mid = $(data.elem).data('mid');
        if(id!=0){
            $("#unitmin_id").empty();
            form.render('select');
            $.post("/Z/ZZZ/filterUnitbig_id.php", { id: id,tb: tb,mid: mid }, function(data){
                $("#unitmin_id").html(data);
                form.render('select');
            });
        }
    });
    form.on('select(unitbig2_idFilter)', function(data){
        var id = data.value;
        var tb = $(data.elem).data('tb');
        var mid = $(data.elem).data('mid');
        if(id!=0){
            $("#unitmin2_id").empty();
            form.render('select');
            $.post("/Z/ZZZ/filterUnitbig_id.php", { id: id,tb: tb,mid: mid }, function(data){
                $("#unitmin2_id").html(data);
                form.render('select');
            });
        }
    });
    form.on('select(grp_idFilter)', function(data){
        var id = data.value;
        var tb = $(data.elem).data('tb');
        var mid = $(data.elem).data('mid');
        if(id!=0){
            $("#tt_id").empty();
            form.render('select');
            $.post("/Z/ZZZ/filterGrp_id.php", { id: id,tb: tb,mid: mid }, function(data){
                $("#tt_id").html(data);
                form.render('select');
            });
        }
    });
});

优化后

document.writeln("<div id=\'outerdiv\' style=\'position:fixed;top:0;left:0;background:rgba(0,0,0,0.7);z-index:2;width:100%;height:100%;display:none;\'>");
document.writeln("<div id=\'innerdiv\' style=\'position:absolute;\'>");
document.writeln("<img id=\'bigimg\' style=\'border:5px solid #fff;\' src=\'\' /></div></div>");

layui.use(['form', 'layer'], function() {
    var form = layui.form;
    var layer = layui.layer;
    var $ = layui.jquery; // 统一使用 layui.jquery

    // 页面加载完成后执行
    $(function() {
        // 图片放大功能
        $(".pimg").click(function() {
            var _this = $(this);
            imgShow("#outerdiv", "#innerdiv", "#bigimg", _this);
        });

        // 列表展开/收缩
        $('#opener').click(() => $('.ui-content').addClass('layui-show'));
        $('#toggle').click(() => $('.ui-content').toggleClass('layui-show'));

        // 多行文本域自动高度
        $.fn.autoHeight = function() {
            return this.each(function() {
                const resize = () => {
                    this.style.height = 'auto';
                    this.style.height = (this.scrollHeight) + 'px';
                };
                resize();
                $(this).on('keyup input', resize);
            });
        };
        $('textarea[autoHeight]').autoHeight();
        // 禁用拼写检查
        $('textarea').prop('spellcheck', false);

        // 指定渲染容器
        const mathContainers = document.querySelectorAll(".katex");
        // 遍历所有容器
        mathContainers.forEach(container => {
            renderMathInElement(container, {
                delimiters: [
                    { left: '```math', right: '```', display: true },
                    { left: '`$', right: '$`', display: false }
                ],
                throwOnError: false
            });
        });
    });

    // 图片放大方法
    function imgShow(outerdiv, innerdiv, bigimg, _this) {
        var src = _this.attr("src");
        $(bigimg).attr("src", src);
        $("<img/>").attr("src", src).load(function() {
            var windowW = $(window).width();
            var windowH = $(window).height();
            var realWidth = this.width;
            var realHeight = this.height;
            var imgWidth, imgHeight;
            var scale = 0.9;

            if (realHeight > windowH * scale) {
                imgHeight = windowH * scale;
                imgWidth = imgHeight / realHeight * realWidth;
                if (imgWidth > windowW * scale) {
                    imgWidth = windowW * scale;
                }
            } else if (realWidth > windowW * scale) {
                imgWidth = windowW * scale;
                imgHeight = imgWidth / realWidth * realHeight;
            } else {
                imgWidth = realWidth;
                imgHeight = realHeight;
            }

            $(bigimg).css("width", imgWidth);
            var w = (windowW - imgWidth) / 2 - 5;
            var h = (windowH - imgHeight) / 2 - 5;
            $(innerdiv).css({ "top": h, "left": w });
            $(outerdiv).fadeIn("fast");
        });

        $(outerdiv).click(function() {
            $(this).fadeOut("fast");
        });
    }

    // 删除数据方法
    window.setDelete = function(v, n) {
        var url = window.location.href;
        var path = encodeURIComponent(url);
        var value = window.atob(decodeURIComponent(v));
        var name = window.atob(decodeURIComponent(n));

        layer.confirm('你确定要删除' + name + '-' + value + '的这条信息吗', {
            btn: ['确定', '取消']
        }, function() {
            location.href = "/Z/ZZZ/setDelete.php?f=" + v + "&t=" + n + "&p=" + path;
        }, function() {
            layer.msg('已取消操作!');
        });
    };

    // 临时标识恢复默认值
    window.setFlag = function(v, n) {
        var url = window.location.href;
        var path = encodeURIComponent(url);
        var value = window.atob(decodeURIComponent(v));
        var name = window.atob(decodeURIComponent(n));

        layer.confirm('你确定要' + name + '的临时标识全修改为' + value + '吗', {
            btn: ['确定', '取消']
        }, function() {
            location.href = "/Z/ZZZ/setFlag.php?f=" + v + "&t=" + n + "&p=" + path;
        }, function() {
            layer.msg('已取消操作!');
        });
    };

    // 临时序号恢复默认值
    window.setSort = function(v, n) {
        var url = window.location.href;
        var path = encodeURIComponent(url);
        var value = window.atob(decodeURIComponent(v));
        var name = window.atob(decodeURIComponent(n));

        layer.confirm('你确定要' + name + '的临时序号全修改为' + value + '吗', {
            btn: ['确定', '取消']
        }, function() {
            location.href = "/Z/ZZZ/setSort.php?f=" + v + "&t=" + n + "&p=" + path;
        }, function() {
            layer.msg('已取消操作!');
        });
    };

    // 自定义处理
    window.setTable = function(str) {
        var url = window.location.href;
        var path = encodeURIComponent(url);

        layer.confirm(decodeURIComponent(str) + '<br>你确定要修改吗', {
            btn: ['确定', '取消']
        }, function() {
            location.href = "/Z/ZZZ/setTable.php?f=" + str + "&p=" + path;
        }, function() {
            layer.msg('已取消操作!');
        });
    };

    // 下拉菜单二级联动(添加防抖处理)
    function handleSelectChange(selectId, targetId, filterUrl) {
        form.on('select(' + selectId + ')', debounce(function(data) {
            const id = data.value;
            const tb = $(data.elem).data('tb');
            const mid = $(data.elem).data('mid');

            if (id === '0') return;

            const $target = $(targetId);
            $target.empty().append('<option value="">加载中...</option>');
            form.render('select');

            $.post(filterUrl, { id, tb, mid }, function(data) {
                $target.html(data);
                form.render('select');
            }).fail(function() {
                $target.html('<option value="">加载失败</option>');
                form.render('select');
            });
        }, 300));
    }

    // 防抖函数
    function debounce(func, delay) {
        let timer;
        return function() {
            const context = this;
            const args = arguments;
            clearTimeout(timer);
            timer = setTimeout(() => func.apply(context, args), delay);
        };
    }

    // 注册所有下拉菜单事件
    handleSelectChange('cbig_idFilter', '#cmin_id', '/Z/ZZZ/filterCbig_id.php');
    handleSelectChange('c2big_idFilter', '#c2min_id', '/Z/ZZZ/filterC2big_id.php');
    handleSelectChange('unitbig_idFilter', '#unitmin_id', '/Z/ZZZ/filterUnitbig_id.php');
    handleSelectChange('unitbig2_idFilter', '#unitmin2_id', '/Z/ZZZ/filterUnitbig_id.php');
    handleSelectChange('grp_idFilter', '#tt_id', '/Z/ZZZ/filterGrp_id.php');
});
posted @ 2025-05-19 08:06  onestopweb  阅读(9)  评论(0)    收藏  举报