我有以下代码
<a href="#go">go</a>
<a href="#go2">go</a>
<a href="#go3">go</a>
<a href="#go4">go</a>
<div id="go" class=contents>1</div>
<div id="go2" class=contents>2</div>
<div id="go3" class=contents>3</div>
<div id="go4" class=contents>4</div>
Run Code Online (Sandbox Code Playgroud)
和一个由锚点发射的脚本
$('a').click(function() {
$($(this).attr('href')).css('color', 'red');
});
Run Code Online (Sandbox Code Playgroud)
所以当点击其中一个锚时,我可以发射一些事件,我的问题是.如果我刷新页面(URL将包含#go标签)是否可以#go从URL中捕获并触发相同的事件?
当然,在页面加载时,您可以捕获哈希并像这样触发事件处理程序
$(function() {
var hash = window.location.hash;
var anchor = $('[href="' + hash + '"]');
anchor.trigger('click');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
349 次 |
| 最近记录: |