如何用google chart api url改变img标签的src属性?
一开始,我在html正文中设置了这个属性。
<img class="image2" />
然后,我想用图表URL来改变Src属性。
到目前为止,我已经尝试过用JQuery
$("#image2").attr('src', 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World');
Javascript
var image = document.getElementsByClassName("image2");
image.src = "https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World"
但是没有显示图像。
可以这样做吗?谢谢
当使用jquery通过classname选择时,使用句号代替磅号。
<img class="image2" />
$(".image2")
磅号是用于id选择器
<img id="image2" />
$("#image2")
请看下面的工作片段...
$(".image2").attr('src', 'https://chart.googleapis.com/chart?cht=p3&chs=250x100&chd=t:60,40&chl=Hello|World');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="image2" />