Javascript 未捕获的类型错误:无法读取空错误的属性“textContent”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32973616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:27:20  来源:igfitidea点击:

Uncaught TypeError: Cannot read property 'textContent' of null error

javascriptd3.jsdc.jscrossfilter

提问by aakashgupta.0205

I am trying to use dc.js to implement the crossfilter and d3. I am almost successful. When I run my code in JSFiddle, it works perfectly ok! But when i try to implement the exact code on the local system, it gives me Uncaught TypeError: Cannot read property 'textContent' of nullerror.

我正在尝试使用 dc.js 来实现 crossfilter 和 d3。我几乎成功了。当我在 JSFiddle 中运行我的代码时,它工作得很好!但是当我尝试在本地系统上实现确切的代码时,它给了我Uncaught TypeError: Cannot read property 'textContent' of null错误。

My code is

我的代码是

trial1.js

试用1.js

var yearChart = dc.barChart('#year-chart');

//the data
var data = [
{date: "2015-10-01", type: "car", quantity: 3}];

var dispatch = crossfilter(data);

var parseDate = d3.time.format("%Y-%m-%d").parse;
data.forEach(function (d) {
    d.date = parseDate(d.date);
    d.quantity = +d.quantity;
    d.Year = d.date.getFullYear();
});

var dateDim = dispatch.dimension(function (d) {
    return d.date;
});
var productions = dateDim.group().reduceSum(function (d) {
    return d.quantity;
});
var minDate = dateDim.bottom(1)[0].date;
var maxDate = dateDim.top(1)[0].date;

yearChart.width(2000).height(200)
    .dimension(dateDim)
    .group(productions)
    .x(d3.time.scale().domain([minDate, maxDate]))
    .brushOn(false)
 .centerBar(true)
    .yAxisLabel("Productions per day")
 .xUnits(function(){return 10;});

yearChart.render();
<html>
 <head>
 <meta charset="utf-8">
  <script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" charset="utf-8"></script>
  <script type="text/javascript" src="https://dc-js.github.io/dc.js/js/d3.js"></script>
  <script type="text/javascript" src="https://dc-js.github.io/dc.js/js/crossfilter.js"></script>
  <script type="text/javascript" src="https://dc-js.github.io/dc.js/js/dc.js"></script>
  <script type="text/javascript" src="https://dc-js.github.io/dc.js/js/colorbrewer.js"></script>
  <script type="text/javascript" src = "trial1.js"></script>
 </head>
 <body>
 <div id="year-chart"></div>
 </body>
</html>

the line of code in d3.jsthat gives the error is this.node().textContent;

d3.js中给出错误的代码行是this.node().textContent;

回答by Alvaro Joao

I had the same issue! localyJust found out that the:

我遇到过同样的问题!localy刚刚发现:

 <div id="year-chart"></div>
 <div id="year-chart"></div>

was misplaced!

放错了地方

just try to setup allyour html content beforethe JSfiles. And will work fine!

只需尝试在文件之前设置所有html 内容。并且会正常工作!JS

see the example:

看例子:

obs: It was working on JSFiddle because the html (must) rend before the jsscripts!

obs:它在 JSFiddle 上工作,因为 html(必须)在js脚本之前呈现!

Hopefully will work for you too.

希望对你也有用。

<!DOCTYPE html>
<html lang="en">
<head>
    <title>dc.js - Bar Chart Example</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="http://dc-js.github.io/dc.js/css/dc.css"/>
    <script src="https:////cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" charset="utf-8"></script>
    <script src="http://dc-js.github.io/dc.js/js/d3.js" charset="utf-8"></script>
    <script src="http://dc-js.github.io/dc.js/js/crossfilter.js"></script>
    <script src="http://dc-js.github.io/dc.js/js/dc.js"></script>
</head>
<body>
    
  
<div id="year-chart"></div> 

<script type="text/javascript">

var yearChart = dc.barChart('#year-chart');

  //the data
  var data = [{
      date: "2015-10-01",
      customer: "BOASG",
      quantity: 3
  }];


  var dispatch = crossfilter(data);

  var parseDate = d3.time.format("%Y-%m-%d").parse;
  data.forEach(function (d) {
      d.date = parseDate(d.date);
      d.quantity = +d.quantity;
      d.Year = d.date.getFullYear();
  });

  var dateDim = dispatch.dimension(function (d) {
      return d.date;
  });
  var productions = dateDim.group().reduceSum(function (d) {
      return d.quantity;
  });
  var minDate = dateDim.bottom(1)[0].date;
  window.alert(minDate);
  var maxDate = dateDim.top(1)[0].date;
  window.alert(maxDate);

  yearChart.width(2000).height(200)
      .dimension(dateDim)
      .group(productions)
      .x(d3.time.scale().domain([minDate, maxDate]))
      .brushOn(false)
      .centerBar(true)
      .yAxisLabel("Productions per day")
      .xUnits(function () {
      return 10;
  });

  dc.renderAll();

</script>
 <!-- <div id="year-chart"></div>  if you set the code here you will be able to reproduce the issue.-->
</body>
</html>