简体   繁体   English

获取内容 <div> 知道它的身份

[英]Get content of <div> knowing its id

I am trying to get the content of a element of which I know partially the id. 我试图得到一个我知道部分id的元素的内容。
The foo variable is correctly generated, but I cannot get it to return a chart object from the div element. foo变量是正确生成的,但是我不能让它从div元素返回一个图表对象。
This is the jQuery code I am using: 这是我正在使用的jQuery代码:

$(".addViewport").click(function () {                   

  var foo = "#" + $(this).parent().children('div[id$="_chart"]').attr('id');

  var chart = foo.CanvasJSChart();

  chart.options.axisX.viewportMaximum += 86400000;
  chart.render();

});

And this is the html: 这是html:

<div id="temperature">
    <div id="temperature_chart" style="height: 300px; width: 100%;"></div>
    <button class="addViewport">Viewport +</button>
</div>

The chart library I am using is called CanvasJS and it has a method called .CanvasJSChart(); 我正在使用的图表库名为CanvasJS,它有一个名为.CanvasJSChart()的方法; to get the chart from specific div element. 从特定div元素获取图表。 截图

This line returns a string ( "#temperature_chart" ): 该行返回一个字符串( "#temperature_chart" ):

var foo = "#" + $(this).parent().children('div[id$="_chart"]').attr('id');

and that's why this line cannot work: 这就是为什么这条线无法工作的原因:

var chart = foo.CanvasJSChart();

foo needs to become a jQuery object in order for all that to work. foo需要成为一个jQuery对象才能使所有这些工作。 For example: 例如:

var foo = $(this).parent().children('div[id$="_chart"]');

This worked for me in JSFiddle: 这在JSFiddle中对我有用:

$(".addViewport").parent().children().get(0)

https://jsfiddle.net/fyvh0o56/ https://jsfiddle.net/fyvh0o56/

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

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