简体   繁体   English

使用jQuery UI加载页面并突出显示锚点div

[英]Load page and highlight anchor div using jQuery UI

I'm trying to setup my anchor tags in a way similar to StackOverflow where if I'm on Page A and click on a link to a div on Page B, the div is highlighted via jQuery UI's, highlight feature. 我试图以类似于StackOverflow的方式设置锚标记,如果我在页面A上并单击指向页面B上div的链接,则该div通过jQuery UI的高亮功能高亮显示 What I'm having trouble figuring out is how to pass the anchor link to the new page and highlight it. 我要弄清楚的是如何将锚链接传递到新页面并将其突出显示。 Here is the page scenario. 这是页面方案。

Page A A页

...
<a href="/albums/1#track-1

Page B 网页B

<!-- /albums/1#track-1 -->

<div class="tracklist">
  ...
  <div id="track-1" class="track">Track 1</div> <!-- Highlights this div -->
  <div id="track-2" class="track">Track 2</div>
  <div id="track-3" class="track">Track 3</div>
  ...
</div>

Aside from the setup itself I have a few performance concerns. 除了设置本身之外,我还有一些性能问题。 Ideally I'm thinking that you'd have to loop through every .track on every page load , regardless if those particular divs are on the page. 理想情况下,我认为您必须.track 每个页面加载中的每个.track ,无论这些特定的div是否在页面上。 What would be the best approach to do this? 这样做的最佳方法是什么?

You wrote wrong the ids in your html code, remove the '#' symbol. 您在html代码中输入了错误的ID,请删除“#”符号。 Something like the following will do the trick. 像下面这样的东西可以解决问题。

$(document).ready(function() {
  var url = window.location.href; // Returns full current url.
  var hash = url.substring(url.indexOf("#") + 1);

  $('#' + hash).effect('highlight', {color: '#00f'}, 2000);
});

https://jsfiddle.net/jonathanzuniga/f6qohfb8/ https://jsfiddle.net/jonathanzuniga/f6qohfb8/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM