简体   繁体   English

如何从html元素获取javascript var数据? (使用canvasjs)

[英]How to get javascript var data from html element? (working with canvasjs)

I am trying to make a chart with CanvasJS. 我正在尝试使用CanvasJS绘制图表。 Here is my HTML: 这是我的HTML:

<div id="chartContainer" style="height: 360px; width: 100%;"></div>
<div id="test1">100</div>
<div id="test2">110</div>

and my JS is 而我的JS是

var dataPoint1 = document.getElementById("test1");
var dataPoint2 = document.getElementById("test2");

var chart = new CanvasJS.Chart("chartContainer",
{  
  data: [
  {
    type: "column",
    dataPoints: [
        { label: "Apple", y: dataPoint1},
        { label: "Mango", y: dataPoint2}
    ]
  }
  ]
});

chart.render();

See my fiddle here : the chart is not correctly rendered. 在这里查看我的小提琴:图表未正确呈现。

You need to add innerHTML in order to access the content in the DOM element. 您需要添加innerHTML才能访问DOM元素中的内容。 Also, you need to then apply parseInt to parse the string in and return an integer. 另外,您还需要应用parseInt来解析字符串并返回整数。

var dataPoint1 = document.getElementById("test1").innerHTML;
var dataPoint2 = document.getElementById("test2").innerHTML;

see your updated jsfiddle here 在这里查看更新的jsfiddle

In your original code you were setting the variables to the whole element, but not the value in the element. 在原始代码中,您将变量设置为整个元素,但未设置元素中的值。 You should always use console.log and then you can see in the console the result you are getting. 您应该始终使用console.log ,然后您可以在控制台中看到所获得的结果。 It is also useful to check typeof what is in the element, as this would have helped you see that it was returning a String as opposed to a Number . 检查typeof元素中的内容也很有用,因为这可以帮助您看到它返回的是String而不是Number

ie console.log(dataPoint1) -- Originally this was returning <div id="test1">10</div> which would have helped you realise you need to get the value in the div, as opposed to the entire element. console.log(dataPoint1) -最初返回的是<div id="test1">10</div> ,这将帮助您认识到需要获取div中的值,而不是整个元素。

 var dataPoint1 = document.getElementById("test1").innerHTML; var dataPoint2 = document.getElementById("test2").innerHTML; var chart = new CanvasJS.Chart("chartContainer", { data: [ { type: "column", dataPoints: [ { label: "Apple", y: parseInt(dataPoint1)}, { label: "Mango", y: parseInt(dataPoint2)} ] } ] }); chart.render(); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/canvasjs/1.7.0/canvasjs.min.js"></script> <div id="chartContainer" style="height: 360px; width: 100%;"></div> <div id="test1">10</div> <div id="test2">12</div> 

I suppose you need value of the element like and convert the values as numbers by prepending a + or use parseInt() : 我想您需要像这样的元素的值,并通过在前面加上+或使用parseInt()将值转换为数字:

var dataPoint1 = +document.getElementById("test1").textContent.trim();
var dataPoint2 = +document.getElementById("test2").textContent.trim();

or: 要么:

var dataPoint1 = parseInt(document.getElementById("test1").textContent.trim(), 10);
var dataPoint2 = parseInt(document.getElementById("test2").textContent.trim(), 10);

You need to retrieve the actual content of those elements, probably by using dataPoint1.innerHTML . 您可能需要使用dataPoint1.innerHTML来检索那些元素的实际内容。 You then need to further parse as an int, with parseInt() 然后,您需要使用parseInt()进一步解析为一个int

See: http://jsfiddle.net/6gawzc74/4/ for my "fix" 参见: http : //jsfiddle.net/6gawzc74/4/作为我的“修复”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM