Node.js 如何在io.js中的SVG文件上添加CSS?

Node.js 如何在io.js中的SVG文件上添加CSS?,node.js,svg,d3.js,jsdom,Node.js,Svg,D3.js,Jsdom,我正在使用io.js、jsdom和D3 我试图将css添加到使用D3生成的SVG文件中,如本例所示 CSS是这样添加的 但是不行,我做错了?有什么想法吗? 这是我的剧本 下面的代码工作正常,也可以打印样式,也许您需要的d3做了一些奇怪的事情?你的产出是多少?有什么问题吗 var jsdom = require("jsdom"); jsdom.env( "", [ 'http://d3js.org/d3.v3.min.js' ], function (err, window) {

我正在使用io.js、jsdom和D3

我试图将css添加到使用D3生成的SVG文件中,如本例所示

CSS是这样添加的

但是不行,我做错了?有什么想法吗? 这是我的剧本

下面的代码工作正常,也可以打印样式,也许您需要的d3做了一些奇怪的事情?你的产出是多少?有什么问题吗

var jsdom = require("jsdom");

jsdom.env(
  "",
  [ 'http://d3js.org/d3.v3.min.js' ],
  function (err, window) {
    var d3 = window.d3;
    var document = window.document;
    var margin = {
        top : 40,
        right : 40,
        bottom : 40,
        left : 40
    };
    var width = 500 - margin.left - margin.right;
    var height = 250 - margin.top - margin.bottom;

    var svg = d3.select(document.body)
      .append("svg")
      .attr("version", 1.1)
      .attr("xmlns", "http://www.w3.org/2000/svg")
      .attr("width",width + margin.left + margin.right)
      .attr("height",height + margin.top + margin.bottom)
      .append("g")
      .attr("transform","translate(" + margin.left + "," + margin.top + ")");

    var style='\n /* <![CDATA[ */\n.bar {\n  fill: steelblue;\n}\n\n.bar:hover {\n  fill: brown;\n}\n\n.title {\n  font: bold 14px "Helvetica Neue", Helvetica, Arial, sans-serif;\n}\n\n.axis {\n  font: 10px sans-serif;\n}\n\n.axis path,\n.axis line {\n  fill: none;\n  stroke: #000;\n  shape-rendering: crispEdges;\n}\n\n.x.axis path {\n  display: none;\n}\n /* ]]> */\n';
    d3.select(document.head).append("style")
      .text(style);

    console.log(window.d3.select(window.document.body).html());
  }
);

代码工作正常,但不是解释css,请选中此@jesuperals,您必须将样式标记添加到文档的头部,而不是正文。编辑代码以反映这一点。它通过添加type=text/css属性来工作,但我希望看到您的示例代码中的问题是基本的,只是缺少在标记样式xD中添加样式属性type='text/css'。从我的要点中检查修订版2