首页 > 解决方案 > 如何使用父 ID 修改子 ID?

问题描述

对于某些背景,我有一组图表显示一个国家(例如英格兰)的数据。我希望能够更新所有这些图表以显示不同国家(例如苏格兰)的数据,只需按一下按钮。

我目前尝试这样做的方式是找到父 ID,然后更改子 ID(这似乎比执行 IF 语句检查子 ID 是什么然后更改它要快得多,因为有超过 20图表)。

到目前为止我的脚本:

document.getElementById("scotlandButton").onclick = function() {myFunction()};

function myFunction() {
    $(document).find('#graph1-first-tab').find('#england_graph').attr('id', 'scotland_graph');
}

html示例:

<button id="scotlandButton">Scotland Data</button>

<div id="graph1-first-tab">
    <canvas id="england_graph"></canvas>
</div>

这些图表的 ID 为“england_graph”和“scotland_graph”。

谢谢!

标签: javascripthtmlcanvasonclickparent-child

解决方案


推荐阅读