gpt4 book ai didi

javascript - OnClick 事件表无法正确触发

转载 作者:行者123 更新时间:2023-12-02 18:04:10 26 4
gpt4 key购买 nike

此代码旨在根据按钮单击来显示和隐藏表格,但是,它似乎无法正确切换它。有人知道我做错了什么吗?

<body>
<br>
<button onclick="toggleTable();" style="position:absolute; top:130px; left:1000; width:200px; height:25px">3-D Model</button>

<br>
</body>

<script>

function toggleTable()
{

var status = document.getElementById("modelTable").style.display;

if (status == 'block') {
document.getElementById("modelTable").style.display="none";
} else {
document.getElementById("modelTable").style.display="block";
}
}

<table id="modelTable" border="1" align="right" style="display:block; position:absolute; top:150px; left:1000;">
<tr>
<th>
<a href="google.com"> 3-D Model </a>
</th>
</tr>
<tr>
<td>
<a>test1</a>

</td>
</tr>
<tr>
<td>
<a>test2</a>
</td>
</tr>
</table>

</script>

最佳答案

您的标记无效,您需要将表格放在正文中

<body>
<br/>
<button onclick="toggleTable();" style="position:absolute; top:130px; left:1000; width:200px; height:25px">3-D Model</button>
<br/>
<table id="modelTable" border="1" align="right" style="display:block; position:absolute; top:150px; left:1000;">
<tr>
<th> <a href="google.com"> 3-D Model </a>

</th>
</tr>
<tr>
<td> <a>test1</a>

</td>
</tr>
<tr>
<td> <a>test2</a>

</td>
</tr>
</table>
<script>
function toggleTable() {
var table = document.getElementById("modelTable");
var status = table.style.display;
if (status == 'block') {
table.style.display = "none";
} else {
table.style.display = "block";
}
}
</script>
</body>

演示:Fiddle

关于javascript - OnClick 事件表无法正确触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20256957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com