DIV CSS 佈局教程網

EasyUI 在 IE9 以下重複載入的排查歷程與解法
編輯:JQuery特效代碼     

EasyUI 在低版本 IE 上有過「同一份元件被載入兩次」的回報。網路上流傳的解法是「把 class 裡的 url 拿掉、改寫在 JavaScript 中」,理由是 class 與 JS 各有一份 url 會導致重複請求。這個說法聽起來合理,但實測並未解決本文遇到的問題。反覆縮小範圍之後,真正的原因其實在別處。

真正的癥結:行內 style

<ul id="eva" style="font-size: 25px;"></ul>

只要在 ul 上寫了行內 style,就會觸發重複載入;把 style 移除,問題立刻消失。也就是說,與其糾結 class 與 JS 的 url 誰優先,不如先檢查元素上有沒有殘留的行內樣式。

完整的 Ajax 動態樹 + DataGrid 範例

<html>
<head>
<title>Tree</title>
<link href="../../Content/JQEasyUI/themes/default/easyui.css" rel="stylesheet" type="text/css" />
<link href="../../Content/JQEasyUI/themes/icon.css" rel="stylesheet" />
<script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery.easyui.min.js" type="text/javascript"></script>
<script src="../../Scripts/easyui-lang-zh_CN.js" type="text/javascript"></script>
<script type="text/javascript">
var NodeText;
var time;

$(function () {
    initTable();
    $("#SelectForm").css("display", "none");
    bindSearcheClick();

    $('#eva').tree({
        url      : '/OEE/GetTree',
        method   : 'get',
        animate  : true,
        checkbox : true
    });
});

// 初始化表格
function initTable() {
    $('#tt').datagrid({
        url         : '/OEE/Details',
        title       : 'OEE',
        width       : 700,
        height      : 360,
        fitColumns  : true,
        idField     : 'Fid',
        loadMsg     : '正在加載設備的信息...',
        pagination  : true,
        singleSelect: false,
        pageSize    : 10,
        pageNumber  : 1,
        pageList    : [10, 20, 30],
        queryParams : {
            NodeResult: NodeText,
            SelectTime: $('#SelectTime').datebox('getValue')
        },
        columns: [[
            { field: 'Num',          title: '設備編號', width: 80, align: "center" },
            { field: 'Name',         title: '設備名稱', width: 80, align: "center" },
            { field: 'EarlyTimeOEE', title: '早班OEE', width: 80, align: "center" },
            { field: 'MiddleTimeOEE',title: '午班OEE', width: 80, align: "center" },
            { field: 'LaterTimeOEE', title: '晚班OEE', width: 80, align: "center" },
            { field: 'DelFlag', title: '操作', width: 80, align: 'center',
              formatter: function (value, row, index) {
                  return "<a Fid='" + row.Fid + "' class='SelectInfo' href='javascript:void(0)'>明細</a>";
              }
            }
        ]],
        onLoadSuccess: function (data) {
            $(".SelectInfo").unbind("click");
            $(".SelectInfo").bind("click", function () {
                doSelect($(this).attr("Fid"), time);
                return false;
            });
        }
    });
}

function doSelect(Fid, Time) {
    $('#SelectFrame').attr('src', '/OEE/GetSelectInfo/' + Fid + '/' + Time);
    $('#SelectForm').css('display', 'block');
    $('#SelectForm').dialog({
        width: 470, height: 470, modal: true,
        title: "顯示明細信息",
        collapsible: true, minimizable: true,
        maximizable: true, resizable: true,
        buttons: [{
            id: 'btnSelect', text: '確定', iconCls: 'icon-add',
            handler: function () { $("#SelectForm").dialog("close"); }
        }]
    });
}

// 綁定查詢按鈕
function bindSearcheClick() {
    $("#linkButtonSearch").click(function () {
        var nodes = $('#eva').tree('getChecked');
        var s = '';
        for (var i = 0; i < nodes.length; i++) {
            if (s != '') s += ',';
            s += nodes[i].id;
        }
        NodeText = s;
        time = $('#SelectTime').datebox('getValue');
        initTable();
    });
}
</script>
</head>
<body>
<div style="width: 1000px">
  <div style="width: 200px; float: left">
    <div class="easyui-panel" style="padding: 5px; border-radius: 5px;">
      <ul id="eva"></ul>
    </div>
  </div>
  <div id="Right">
    <div id="query">
      查詢時間:<input class="easyui-datebox" name="SelectTime" id="SelectTime" />
      <a id="linkButtonSearch" href="javascript:void(0)" class="easyui-linkbutton"
         data-options="iconCls:'icon-search'">查詢</a>
    </div>
    <table id="tt" style="width: 700px;" title="標題" iconcls="icon-edit"></table>
    <div id="SelectForm">
      <iframe id="SelectFrame" src="javascript:void(0)" frameborder="0"
              width="100%" height="100%"></iframe>
    </div>
  </div>
</div>
</body>
</html>

排查這類問題的順序建議

遇到重複載入時,不妨照這個順序檢查:先看 DevTools 的 Network 面板確認是哪個請求被發了兩次、由誰發起;再檢查目標元素上有沒有行內 style 或其他自訂屬性;然後才去懷疑 class 與 JS 的 url 衝突。盲從網路傳言往往會繞遠路,實際比對請求記錄才是最快的線索。

另外提醒,範例中 onLoadSuccess 裡重新 bind 之前先做了 unbind,這個習慣值得保留——EasyUI 每次重繪都會重建 DOM,若不解除舊綁定,事件很容易累積觸發多次。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved