未捕获的类型错误:无法读取未定义的属性“createElementNS”

Uncaught TypeError: Cannot read property 'createElementNS' of undefined

本文关键字:未定义 读取 属性 createElementNS 类型 错误      更新时间:2023-09-26

我正在使用nvd3绘制折线图,当我将div传递给nvd3绘制图表时,它给了我此错误

未捕获的类型错误: 无法读取属性"createElementNS" 的 定义

这是代码:

 var chartDiv = 'line-chart'+ counter++;
   tmpl = $($('#charts-panel-template').clone().html());
                tmpl.find('.feature-line-chart').attr('id', chartDiv);
 var  div=tmpl.find('.feature-line-chart#'+chartDiv);
           chartsPanel.append(tmpl);
    nv.addGraph(function() {
    var chart;
    var width = 1024, height = 500;
    chart = nv.models.lineChart()
            // .color(sparkChart.colors)
            .width(width).height(height);
    //modify the x.axes
        chart.x(function(d,i) { 
              return d.x;
        });
    //giving chart margin
    chart.margin({right: 40});
    $(div).empty();
    //create chart
    var svg = d3.select(div).append('svg')
         .datum(data)
         .transition()
         .duration(500)
         .call(chart)
         .attr('width', width)
         .attr('height', height);

我的问题,

  • 我做错的地方
  • 是我错过了什么

我刚刚遇到了同样的问题。

不能将 jquery 引用传递给 d3.select():

 d3.select($element) //no bueno

你必须传递实际的 DOM 节点,或者像你最终发现的那样传递 id。

d3.select($element[0])

d3.select("#id")

我已经得到了解决方案,尽管 NVD3 olny 需要 id

 var  div=tmpl.find('.feature-line-chart#'+chartDiv);

成为

var  div='#'+chartDiv;