gpt4 book ai didi

Javascript - 使用 anchor 从外部链接显示/隐藏表格?

转载 作者:行者123 更新时间:2023-11-28 09:53:34 24 4
gpt4 key购买 nike

我是 Javascript 的新手,我正在做一个元素。感谢在线帮助网站的帮助,我能够成功显示/隐藏我的表格。

当我单击 h3 元素时,它会打开并将 anchor (在这种情况下为 #1、#2、#3)附加到 URL。

我想使用这个 anchor 元素从另一个网页的外部链接打开特定的表格。 (例如,在主页上,我点击了这个 testing.html#1,我希望它在我到达该页面时自动打开第一个表格)

非常感谢!

JavaScript

                <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
function showonlyone(thechosenone) {
$('.newboxes').each(function(index) {
if ($(this).attr("id") == thechosenone) {
$(this).show(200);
}
else {
$(this).hide(600);
}
});
}
</script>

CSS

<style>
#special1{ display: none }

h3 {text-align: center;
background-color: black;
color: white;
clear: both;
cursor: pointer; }
.newboxes {
display: none;
}

a {text-decoration: none;}
</style>

HTML

    <a id="myHeader1" onclick="javascript:showonlyone('newboxes1');" href="#1"><h3>Table 1</h3></a>
<table border="1" align="center" cellspacing="10px" class="newboxes" id="newboxes1">
<tr>
<td>1</td>
</tr>
</table>

<a id="myHeader2" onclick="javascript:showonlyone('newboxes2');" href="#2"><h3>Table 2</h3></a>
<table border="1" align="center" cellspacing="10px" class="newboxes" id="newboxes2">
<tr>
<td>2</td>
</tr>
</table>

<a id="myHeader3" onclick="javascript:showonlyone('newboxes3');" href="#3"><h3>Table 3</h3></a>
<table border="1" align="center" cellspacing="10px"class="newboxes" id="newboxes3">
<tr>
<td>3</td>
</tr>
</table>

最佳答案

请注意,这仅在您从同一域中的 html 页面加载时才有效。

JQuery 的.load 函数非常通用。要从 testing.html 加载第一个表,我们可以这样做:

$('#tableContainer').load('testing.html table:eq(0)');

第二张表:

$('#tableContainer').load('testing.html table:eq(1)');

等等。

demo请注意,演示中的 3 个表是从 here 加载的

如果URL以#1结尾,需要自动执行showonlyone('newboxes1'):

if (window.location.hash.substr(1) == '1') {
showonlyone('newboxes1');
}

关于Javascript - 使用 anchor 从外部链接显示/隐藏表格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988383/

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