矢志不渝Æ 2022-06-19 18:28 采纳率: 33.3%
浏览 62
已结题

这种情况的js乱码怎么解决

朋友们,帮我看看,我外部进入js代码,在浏览器是中文乱码

img

涉及中文部分我单独提取了,如下


 html += '<span class="page-total">共<span class="number">' + total + '</span>页</span>\
          <span class="page-go">到第<input class="w35 go" id="yeshu" type="text" value="">页</span>\
          <input type="button" class="gray-btn" id="go-search" value="确定">';
        tar.innerHTML = html;
        if(options.callback) {
            // 执行回调
            options.callback(total);

下面是js全部代码

(function(window) {
    /**
     * 分页函数
     
     * @param  {[type]}   cur   当前页
     * @param  {[type]}   total 总页数
     * @param  {[type]}   len   显示多少页数
     * @return {[type]}         [description]
     */
    /*var pagination = {
        initPage: function(cur, total, len) {

        }
    }*/
    var pagination = function(options) {
        cur = parseInt(options.cur||1, 10);
        total = parseInt(options.total||0, 10);
        len = parseInt(options.len||0, 10);
        tar = document.getElementById(options.targetId);
        if(!tar || !total) {
            return '';
        }
        // 总数1页
        // cur 当前页,total总页数, len显示多少
        // 第一页,   1、总共不超过len条(下一页),2、超过5条(下一页  右more)
        // 中间       上一页/下一页         上一页/下一页  左右more
        // 最后一页   1、总数不超过5条(上一页),2、超过5条(上一页,左more)
        var html = '<ul class="pages"><li class="page">';
        if (total === 1) {
            html += '<a href="javascript:void(0);" class="page-index">' + 1 + '</a>'
        }
        if (cur !== 1 && total !== 1) {
            // 上一页
            html += '<a href="javascript:void(0);" class="prev page-index" data-index="' + (cur - 1) + '">&lt;</a>'
        }
        if (total > len && cur > Math.ceil(len / 2) && total !== 1) {
            // 左more
            html += '<span class="more">...</span>';
        }
        if (total !== 1) {
            // len 5 total 10  23456  10 6  4-6+1  5678  4-6 5   5-7 5678  7-7  45678910     4 2  53   len-cur+j
            // curr > Math.floor(len/2)    5   var a = Math.floor(len/2);  cur-a cur-a+1 cur-a+2
            // cur > 14    12 13 14 15 16
            var _l = len > total ? total : len;
            if (len < total && cur > Math.floor(len / 2) && (cur <= total - Math.floor(len / 2))) {
                for (var j = 1; j <= len; j++) {
                    if (cur === (cur - Math.ceil(len / 2) + j)) {
                        html += '<a href="javascript:void(0);" class="page-index cur" data-index="' + (cur - Math.ceil(len / 2) + j) + '">' + (cur - Math.ceil(len / 2) + j) + '</a>';
                        continue;
                    }
                    html += '<a href="javascript:void(0);" class="page-index" data-index="' + (cur - Math.ceil(len / 2) + j) + '">' + (cur - Math.ceil(len / 2) + j) + '</a>';
                }
            } else if (cur < len) {
                for (var i = 1; i <= _l; i++) {
                    if (cur === i) {
                        html += '<a href="javascript:void(0);" class="cur page-index" data-index="' + i + '">' + i + '</a>'
                        continue;
                    }
                    html += '<a href="javascript:void(0);" class="page-index" data-index="' + i + '">' + i + '</a>'
                }
            } else {
                for (var i = len - 1; i >= 0; i--) {
                    if (cur === (total - i)) {
                        html += '<a href="javascript:void(0);" class="cur page-index" data-index="' + (total - i) + '">' + (total - i) + '</a>'
                        continue;
                    }
                    html += '<a href="javascript:void(0);" class="page-index" data-index="' + (total - i) + '">' + (total - i) + '</a>'
                }
            }
        }

        if (total > len && cur < total - Math.floor(len / 2) && cur !== total && total !== 1) {
            // 右more
            html += '<span class="more">...</span>';
        }

        if (cur !== total && total !== 1) {
            // 下一页
            html += '<a href="javascript:void(0);" class="page-index next" data-index="' + (cur + 1) + '">&gt;</a>';
        }
        html += '<span class="page-total">共<span class="number">' + total + '</span>页</span>\
          <span class="page-go">到第<input class="w35 go" id="yeshu" type="text" value="">页</span>\
          <input type="button" class="gray-btn" id="go-search" value="确定">';
        tar.innerHTML = html;
        if(options.callback) {
            // 执行回调
            options.callback(total);
        }
    }

    // cmd || node.js
    if ( typeof module === "object" && module && typeof module.exports === "object" ) {
        module.exports = pagination;
    } else {
        window.pagination = pagination;
        // amd
        if ( typeof define === "function" && define.amd ) {
            define( "pagination", [], function () { return pagination; } );
        }
    }
})(window)


这是页面引入方式

<head>
    <meta charset="utf-8">
    <title>当前借阅</title>

    <script src="${pageContext.request.contextPath}/js/pagination.js"></script>
  
</head>


最后是,除了那部分中文乱码,还有整个中文注释都是乱码

(function(window) {
    /**
     * 鍒嗛〉鍑芥暟
     * @date   2015-12-22
     * @param  {[type]}   cur   褰撳墠椤�
     * @param  {[type]}   total 鎬婚〉鏁�
     * @param  {[type]}   len   鏄剧ず澶氬皯椤垫暟
     * @return {[type]}         [description]
     */
    /*var pagination = {
        initPage: function(cur, total, len) {

        }
    }*/
    var pagination = function(options) {
        cur = parseInt(options.cur||1, 10);
        total = parseInt(options.total||0, 10);
        len = parseInt(options.len||0, 10);
        tar = document.getElementById(options.targetId);
        if(!tar || !total) {
            return '';
        }
        // 鎬绘暟1椤�
        // cur 褰撳墠椤碉紝total鎬婚〉鏁帮紝 len鏄剧ず澶氬皯
        // 绗竴椤碉紝   1銆佹�诲叡涓嶈秴杩噇en鏉★紙涓嬩竴椤碉級锛�2銆佽秴杩�5鏉★紙涓嬩竴椤�  鍙砿ore锛�
        // 涓棿       涓婁竴椤�/涓嬩竴椤�         涓婁竴椤�/涓嬩竴椤�  宸﹀彸more
        // 鏈�鍚庝竴椤�   1銆佹�绘暟涓嶈秴杩�5鏉★紙涓婁竴椤碉級锛�2銆佽秴杩�5鏉★紙涓婁竴椤碉紝宸ore锛�
        var html = '<ul class="pages"><li class="page">';
        if (total === 1) {
            html += '<a href="javascript:void(0);" class="page-index">' + 1 + '</a>'
        }
        if (cur !== 1 && total !== 1) {
            // 涓婁竴椤�
            html += '<a href="javascript:void(0);" class="prev page-index" data-index="' + (cur - 1) + '">&lt;</a>'
        }
        if (total > len && cur > Math.ceil(len / 2) && total !== 1) {
            // 宸ore
            html += '<span class="more">...</span>';
        }

救命
  • 写回答

5条回答 默认 最新

  • 虎子8 2022-06-19 19:18
    关注

    应该是文件编码不对,改一下

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 11月11日
  • 已采纳回答 11月3日
  • 创建了问题 6月19日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装