【问题标题】:D3 is giving me a syntax error with d3.selectAll()D3 使用 d3.selectAll() 给我一个语法错误
【发布时间】:2012-10-29 16:07:00
【问题描述】:

我的场景是这样的: 我正在尝试从类列表中选择一个类,该类以数字开头,但类型为字符串(我检查过)。

list_item.on("click", function(){
  var to_find = $(this).text().replace(/\s/g, "_");

  console.log(typeof(to_find)); // on a certain moment this will become "00s"

  sampleSVG.selectAll(".node:not(."+to_find+") circle, .line:not(."+to_find+")")
    .transition()
    .duration(500)
    .style("fill", st_colours.node.fill)
    .style("stroke", function(d){return d.type == "node" ? st_colours.node.stroke : st_colours.line.stroke});

  sampleSVG.selectAll(".node."+to_find+" circle, .line."+to_find)
    .transition()
    .duration(1000)
    .style("fill", st_colours.random(a))
    .style("stroke", d3.rgb(st_colours.random(a)).darker());
});

当“var to_find”变为“00s”时,我收到此错误:

SyntaxError: An invalid or illegal string was specified

谁能告诉我为什么会出现这个错误?

【问题讨论】:

    标签: javascript jquery syntax-error d3.js


    【解决方案1】:

    在您的选择器中,您指的是一个名为 00s 的类:

    .node:not(.00s)
    

    根据 CSS 语法,类名应以下划线、破折号或字母开头。也许这就是您遇到 SyntaxError 的原因。

    另请参阅:Which characters are valid in CSS class names/selectors?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多