【问题标题】:Expected an identifier and instead saw '<'需要一个标识符,而是看到“<”
【发布时间】:2016-05-17 19:59:23
【问题描述】:

我一开始就收到这个错误。不知道如何修复。我还得到了预期的赋值或函数调用,而是看到了一个表达式。更不用说期望标识符而是看到“var”

<script>
var interactiveSearch = {};
(function() {
  interactiveSearch.common = {
    init: function() {
      interactiveSearch.common.setupDataTableDefaults();
      $.ajaxSetup({
        cache: false
      });
    },
    setupDataTableDefaults: function() {
      $.extend($.fn.dataTable.defaults, {
        "sDom": "<'table-header-controls'<'row'<l><f>>r><i>t<'table-footer-controls'<'row'<'span12'p><'span12'i>>>",
        "sPaginationType": "bootstrap",
        "bJQueryUI": false,
        "bProcessing": false,
        "bServerSide": true,
        "fnServerData": interactiveSearch.common.getTableData
      });
    },
    getTableData: function(sSource, aoData, fnCallback) {
      var data = new Array();
      var columnCount = _.find(aoData, function(o) {
        return o.name == 'iColumns';
      }).value;
      var echo = _.find(aoData, function(o) {
        return o.name == 'sEcho';
      }).value;
      var skip = _.find(aoData, function(o) {
        return o.name == 'iDisplayStart';
      }).value;
      var take = _.find(aoData, function(o) {
        return o.name == 'iDisplayLength';
      }).value;
      var search = _.find(aoData, function(o) {
        return o.name == 'sSearch';
      }).value;
      var sortCols = _.filter(aoData, function(o) {
        return o.name.indexOf('iSortCol_') == 0;
      });
      var sortDirs = _.filter(aoData, function(o) {
        return o.name.indexOf('sSortDir_') == 0;
      });
      var searches = _.filter(aoData, function(o) {
        return o.name.indexOf('sSearch_') == 0;
      });
      data.push({
        "name": "TableEcho",
        "value": echo
      });
      data.push({
        "name": "Skip",
        "value": skip
      });
      data.push({
        "name": "Take",
        "value": take
      });
      data.push({
        "name": "AllSearch",
        "value": search
      });
      var actual = 0;
      _.each(sortCols, function(columnSort, sortIndex) {
        var columnIndex = columnSort.value;
        var columnSearch = searches[columnIndex].value;
        var sortDir = sortDirs[sortIndex].value;
        data.push({
          "name": "Columns[" + actual + "].ColumnIndex",
          "value": columnIndex
        });
        data.push({
          "name": "Columns[" + actual + "].SortDirection",
          "value": sortDir
        });
        if (columnSearch != '') {
          data.push({
            "name": "Columns[" + actual + "].SearchTerm",
            "value": columnSearch
          });
        }
        actual++;
      });
      for (var i = 0; i < columnCount; i++) {
        var searchTerm = searches[i].value;
        if (searchTerm == '') {
          continue;
        }
        data.push({
          "name": "Columns[" + actual + "].ColumnIndex",
          "value": i
        });
        data.push({
          "name": "Columns[" + actual + "].SearchTerm",
          "value": searchTerm
        });
        actual++;
      }
      $.post(sSource, data)
        .success(fnCallback);
    }
  };
})();
$(function() {
  interactiveSearch.common.init();
});
(function() {
  var product = interactiveSearch.product = {};
  product.init = function() {
    product.initDataTable();
    product.bindEvents();
  };

  function convertFullRowToDataObject(fullRow) {
    return {
      Id: fullRow[0],
      ProductName: fullRow[1],
      Synonym: fullRow[2],
      Acronym: fullRow[3],
      CasNo: fullRow[4],
      EinecsNo: fullRow[5],
      Formula: fullRow[6],
      MolecularWeight: fullRow[7],
      Status: fullRow[8],
      MeltingPoint: fullRow[9],
      BoilingPoint: fullRow[10],
      HasDoc: fullRow[11] !== '',
      RelatedDocPath: product.baseUrl + fullRow[11],
      HasDImage: fullRow[12] !== '',
      ImagePath: product.baseUrl + fullRow[12]
    };
  }
  product.initDataTable = function() {
    product.productTable = $("#product-table").dataTable({
      aaSorting: [
        [1, "asc"]
      ],
      iDisplayLength: 15,
      bServerSide: true,
      bDestroy: true,
      sAjaxSource: interactiveSearch.product.listUrl,
      fnRowCallback: function(nRow, aData) {
        $(nRow).data('rowInfo', convertFullRowToDataObject(aData));
      },
      aoColumns: [{
        sType: "string",
        sClass: "dtAlignLeft",
        mData: 1
      }]
    });
  };
  product.bindEvents = function() {
    _.templateSettings = {
      interpolate: /\{\{(.+?)\}\}/g,
      evaluate: /\{\[([\s\S]+?)\]\}/g
    };
    var templateText = $('#productDetailTemplate').html(),
      compiledTemplate = _.template(templateText);
    $(document).on('click', '#product-table tr', function(e) {
      var el = $(this);
      var rowData = el.data('rowInfo');
      var html = compiledTemplate(rowData);
      $('#productDetailContainer').empty().html(html);
      $('#product-table tr').removeClass('active');
      el.addClass('active');
    });
    $('#searchClone').on('keyup', function(e) {
      var el = $(this);
      var mimicEl = $('#product-table_filter input');
      mimicEl.val(el.val()).trigger('keyup');
    })
    $('.btn-reset-filter').on('click', function() {
      $('#searchClone').val('').trigger('keyup');
    });
  };
})();
$(document).ready(function() {
  interactiveSearch.product.listUrl = '/pa/Product/ListItems';
  interactiveSearch.product.baseUrl = '/pa/';
  interactiveSearch.product.init();
});
</script>

【问题讨论】:

  • 所有这些都在.js 文件中吗?
  • 你在哪一行得到了错误?
  • 如果这是在 .js 文件中,则代码周围不应有 &lt;script&gt;&lt;/script&gt;。这仅在您将 Javascript 嵌入 HTML 文件时使用。
  • 第一行三个,第二行一个
  • @gdeleon101 如果该代码在 HTML 文件中,那么这不是违规代码。所有这些代码在语法上都是有效的。

标签: javascript


【解决方案1】:

在 .js 文件中您不必输入&lt;script&gt;,您只需编写代码即可。

&lt;script&gt; 用于 HTML 文件,当您必须在页面中间插入脚本时。

所以你必须删除文件中的&lt;script&gt;&lt;/script&gt;

【讨论】:

  • 我不确定这是如何回答用户@gdeleon101 本人在 [above cmets]:(stackoverflow.com/questions/37285211/…) 中回答的问题。 This code was taken from inside of an HTML page. It is not a stand alone .js file. Client is getting errors, and I'm trying to figure out why
猜你喜欢
  • 2019-05-07
  • 2018-10-18
  • 1970-01-01
  • 2018-09-07
  • 1970-01-01
  • 1970-01-01
  • 2014-11-27
  • 2023-03-11
  • 1970-01-01
相关资源
最近更新 更多