想在其他html页面上只运行匹配id链接吗
Want to Run only match id links on other html page?
anchor_example1.html
这是第一页代码
h1>Menu page</h1>
<h2>Anchor example</h2>
<h3><a name="menu">Menu</a></h3>
<ul>
<li><a href="anchor_example2.html#a001">Jump to a001</a></li>
<li><a href="anchor_example2.html#a002">Jump to a002</a></li>
<li><a href="anchor_example2.html#a003">Jump to a003</a></li>
</ul>
anchor_example2.html
这是第二页代码
<h1>Target page</h1>
<h2>Anchor example</h2>
<h3><a name="a001">a001</a></h3>
<p>paragraph text ...</p>
<h3><a name="a002">a002</a></h3>
<p>paragraph text ...</p>
<h3><a name="a003">a003</a></h3>
<p>paragraph text ...</p>
<hr>
当我点击第一个href链接时,它也会在第二页上打开所有其他链接。但我希望它在第一个href连接选择on时根据指定的id#a001示例运行。
<li><a href="anchor_example2.html#a001">Jump to a001</a></li>
然后它只运行第二页上的第一部分,所有其他部分都将隐藏。
<h3><a name="a001">a001</a></h3><p>paragraph text ...</p>
如果您正在寻找一个基于jquery的解决方案,它将完全隐藏其他部分,而不是您通过url指定的部分,那么下面的解决方案是可行的。
anchor_example1.html
<h1>Menu page</h1>
<h2>Anchor example</h2>
<h3><a name="menu">Menu</a></h3>
<ul>
<li><a href="anchor_example2.html#a001">Jump to a001</a></li>
<li><a href="anchor_example2.html#a002">Jump to a002</a></li>
<li><a href="anchor_example2.html#a003">Jump to a003</a></li>
</ul>
anchor_example2.html
<h1>Target page</h1>
<h2>Anchor example</h2>
<div class="content" name="a001">
<h3><a>a001</a></h3>
<p>paragraph text ...</p>
</div>
<div class="content" name="a002">
<h3><a>a002</a></h3>
<p>paragraph text ...</p>
</div>
<div class="content" name="a003">
<h3><a>a003</a></h3>
<p>paragraph text ...</p>
</div>
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$(".content").hide();
var currentUrl = window.location.href;
var anchorVal = currentUrl.split("#")[1];
$('.content[name='+anchorVal+']').show();
})
</script>
您应该通过id
而不是name
来指定它
相关文章:
- 如何添加动态Id's并在运行时提取它们
- Chrome扩展插件:runtime.last运行tabs.get/tabs.remove时出错:没有id为0的选项卡
- 数据互绑定问题:转换器只运行一次,无法绑定元素的 ID
- 更新脚本以针对具有递增 ID 的新创建的表行运行
- jQuery更改ID赢得't运行函数
- 使用 javascript 在运行时将 id 添加到
- 标签
- 如何选择当前 DIV ID 以使用该 ID 运行 Jquery
- 为数组中的每个间隔 ID 再次运行设置间隔
- db.collection findOne by id 运行但不返回
- 从 .each 返回的“undefined”在 T 上运行,并选择了 ID
- Chrome扩展错误:“;未选中runtime.last运行browserAction.setIcon时出错:没有id为
- 如何在javascript id选择器中使用if条件.我不想运行'window.addeventlistener
- 使用触发器多次运行具有不同id的插件
- 将id替换为类,现在Javascript无法运行
- jQuery触发事件的id的元素运行处理程序定义的类在包含的小枝模板
- 如何附加点击事件在运行时,当我有一个数组中的所有id得到填充时,文档准备好了
- 如果一个ID等于"X"或“;Y"不要运行函数
- 想在其他html页面上只运行匹配id链接吗
- 为什么要更改HTML元素的id,使用旧id的点击绑定也会运行
- Wordpress中的条件标签:如何根据页面ID或标题运行不同的JS文件