我正在尝试在选择中列出一些 amChart 图表。它没有任何默认图表,因此我尝试为 <select>
设置默认值通过对其中一个选项使用 selected 属性,它会在选择框中被选中,但不会出现在页面上。
var currentChart;
function showChart( divid ) {
if (currentChart !== undefined)
currentChart.style.display = "none";
if ( divid ) {
currentChart = document.getElementById(divid);
currentChart.style.display = "block";
}
else {
currentChart = undefined;
}
}
<select onchange="showChart(this.options[this.selectedIndex].value);">
<option value="">Select the chart to show</option>
<option value="chart1" selected>Chart #1</option>
<option value="chart2">Chart #2</option>
<option value="chart3">Chart #3</option>
</select>
<div id="chart1" class="chartBoxSingle" style="display: none;">Chart 1</div>
<div id="chart2" class="chartBoxSingle" style="display: none;">Chart 2</div>
<div id="chart3" class="chartBoxSingle" style="display: none;">Chart 3</div>
最佳答案
您只能对标签使用 onload 事件
<body>, <frame>, <iframe>, <img>, <input type="image">, <link>,
<script>, <style>
对于其他元素,您可以通过两种方式执行
在元素后面插入包含要在元素加载时执行的函数的脚本标记或
使用jquery
$(document).ready(function() { showChart('chart1'); });
<script>
var currentChart;
function showChart(divid) {
if (currentChart !== undefined)
currentChart.style.display = "none";
if (divid) {
currentChart = document.getElementById(divid);
currentChart.style.display = "block";
} else {
currentChart = undefined;
}
}
</script>
<select onchange="showChart(this.options[this.selectedIndex].value);">
<option value="">Select the chart to show</option>
<option value="chart1" selected>Chart #1</option>
<option value="chart2">Chart #2</option>
<option value="chart3">Chart #3</option>
</select>
<div id="chart1" class="chartBoxSingle" style="display: none;">char1</div>
<div id="chart2" class="chartBoxSingle" style="display: none;">char2</div>
<div id="chart3" class="chartBoxSingle" style="display: none;">char3</div>
<script type="text/javascript">
showChart('chart1');
</script>
关于javascript - Html <select> selected 属性在 jQuery 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49108207/