未捕获的语法错误:意外的令牌 .D3 工具提示



我遇到此错误的问题。我正在尝试向 d3 元素添加工具提示。如果您发现我哪里出错,请告诉我。错误说

Uncaught SyntaxError: Unexpected token . on the .append

这是我的代码:

g.selectAll("scatter-dots")
  .data(data)
  .enter().append("svg:circle")
      .attr("cx", function (d,i) { return x(d[0]); } )
      .attr("cy", function (d) { return y(d[1]); } )
      .attr("r", 8);
      // Here we add an SVG title element the contents of which is effectively rendered in a tooltip
      .append("svg:title")
        .text(function(d, i) { return "My color is " + d[2]; });

就像错误说的那样,您有一个语法错误

.attr("r", 8);

不能先;然后追加。 ;实际上结束了语句。这是正确的代码。

g.selectAll("scatter-dots")
  .data(data)
  .enter().append("svg:circle")
      .attr("cx", function (d,i) { return x(d[0]); } )
      .attr("cy", function (d) { return y(d[1]); } )
      .attr("r", 8)
      // Here we add an SVG title element the contents of which is effectively rendered in a tooltip
      .append("svg:title")
        .text(function(d, i) { return "My color is " + d[2]; });

注意:我认为您在选择中也有错误,g.selectAll("scatter-dots") scatter-dots指的是什么? classid。如果是class那么它必须是.scatter-dots的,或者如果它是一个id那么它必须是#scatter-dots的。

最新更新