我正在尝试简化此代码

时间:2016-10-05 14:49:06

标签: javascript jquery html

这段代码可以简化吗?我很确定它可以通过抽象变量并使用if else或switch来实现,每个case都是元素ID ......我很难完成这个。

        function NoTime() {
            if (shortTime == "") {
                timeFormat = "N/A"
            }
        }

        var shortTime;
        var day;
        var month;
        var year;

        var revisionDate = document.getElementById("RevisionDate").value;
        shortTime = revisionDate;
        day = revisionDate.substring(8, 10);
        month = revisionDate.substring(4, 7);
        year = revisionDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#RevisionDate")
            .replaceWith("<div id='RevisionDate' class='col-md-12 margin-bottom-10px pad-L-15px border-1px width-174px pad-LR-3px width-227px' /></div>");
        $("#RevisionDate").html(timeFormat);

        var supplierPartInformationEffectiveDate = document.getElementById("SupplierPartInformationEffectiveDate")
            .value;
        shortTime = supplierPartInformationEffectiveDate;
        day = supplierPartInformationEffectiveDate.substring(8, 10);
        month = supplierPartInformationEffectiveDate.substring(4, 7);
        year = supplierPartInformationEffectiveDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#SupplierPartInformationEffectiveDate")
            .replaceWith("<div id='SupplierPartInformationEffectiveDate' class='col-md-12 margin-bottom-10px pad-LR-3px border-1px pad-LR-3px width-342px' /></div>");
        $("#SupplierPartInformationEffectiveDate").html(timeFormat);

        var SupplierPartInformationExpirationDate = document.getElementById("SupplierPartInformationExpirationDate")
            .value;
        shortTime = SupplierPartInformationExpirationDate;
        day = SupplierPartInformationExpirationDate.substring(8, 10);
        month = SupplierPartInformationExpirationDate.substring(4, 7);
        year = SupplierPartInformationExpirationDate.substring(11, 15);
        var timeFormat = day + " " + month + " " + year;
        NoTime();
        $("#SupplierPartInformationExpirationDate")
            .replaceWith("<div id='SupplierPartInformationExpirationDate' class='col-md-12 margin-bottom-10px pad-LR-3px border-1px pad-LR-3px width-342px' /></div>");
        $("#SupplierPartInformationExpirationDate").html(timeFormat);

1 个答案:

答案 0 :(得分:0)

{{1}}