【问题标题】:ReferenceError: invalid assignment left-hand side in consoleReferenceError:控制台左侧分配无效
【发布时间】:2017-01-08 22:56:54
【问题描述】:

js代码

if ($language == 'english') {
  var displayvalue = "";
  if (data[i].report_name.length >= 20) {
    displayvalue = data[i].report_name.substr(0, 20) + "...";
  } else {
    displayvalue = data[i].report_name
  }
  subMDesign += '<a  href=""  ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22)"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>';

} else {
  var displayvalue = "";
  if (data[i].report_name.length >= 20) {
    displayvalue = data[i].report_name_ar.substr(0, 20) + "...";
  } else {
    displayvalue = data[i].report_name_ar;
  }
  subMDesign += '<a  href=""  ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22)"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>';
}
}
var temp = $compile(subMDesign)($scope);
$("#reportUl" + $catid).html(temp);

我将subMDesign 动态附加到最后一行的div。在控制台中,subMDesign显示如下:

拖动开始时,控制台显示错误如下:

ReferenceError:左侧赋值无效 event.dataTransfer.setData(text, drag.action?reportId=93&reportType=6_93_6_22)

并且箭头标记指向reportId=93。请帮我解决这个问题。

【问题讨论】:

  • 什么?您知道确切的位置,并且错误消息很清楚。您需要什么样的帮助?
  • 但我不知道那个位置有什么问题
  • drag.action?reportId=93&amp;reportType=6_93_6_22 在纯 JS 中做什么?它看起来像是 URL 的一部分,但在您的代码中它实际上是一个赋值。
  • 为reportId和reportType赋值并转到drag.action
  • 进入地址栏时,是的,但不是在 JS 中。 JavaScript 没有 url 数据类型。

标签: javascript jquery css angularjs


【解决方案1】:

这很糟糕,因为您分配了一个字符串而没有指明它;例如,您想在页面上呈现它:

// Note the quotes, to escape the string:
event.dataTransfer.setData(text, 'drag.action?reportId=93&reportType=6_93_6_22')

为了实现这一点,这里有一个简单的重构,它还通过删除重复使您的代码更易于阅读。我注意到英语/非英语之间唯一不同的是报告名称字段:

var displayvalue = "";
var field = ($language == 'english') ? 'report_name' : 'report_name_ar';

if (data[i].report_name.length >= 20) {
  displayvalue = data[i][field].substr(0, 20) + "...";
} else {
  displayvalue = data[i][field];
}

subMDesign += '<a  href=""  ng-click="getIndividualGraph(' + data[i].report_id + ',' + data[i].report_type + ')" ondragstart="event.dataTransfer.setData(text, \'drag.action?reportId=' + data[i].report_id + '&reportType=' + data[i].report_type + '_' + data[i].report_id + '_' + data[i].report_type + '_22\')"><span class="iconsweet">r</span><span class="tooltips" title="' + data[i].report_description + '" unselectable="on">' + displayvalue + '</span></a>';

var temp = $compile(subMDesign)($scope);
$("#reportUl" + $catid).html(temp);

您会看到我添加了转义引号(例如:\')以防止它们终止实际字符串,但导致引号在页面上呈现。

例如:

var foo = "foo"; 
console.log(foo); // prints: foo
var bar = "\"bar\""; // we have to escape double quotes inside double quotes
console.log(bar); // prints: "bar"
var blee = "'blee'"; // no need to escape single quotes inside double quotes
console.log(blee); // prints: 'blee'
var blar = '\'blar\''; // but we have to escape single quotes inside single quotes
console.log(blee); // prints: 'blar'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-07
    • 2018-10-02
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    • 2016-08-16
    • 1970-01-01
    相关资源
    最近更新 更多