开发者

Highlight a # section in a page - jQuery

开发者 https://www.devze.com 2023-03-04 22:53 出处:网络
I have a simple html page <html> <div id=\"d1>Content开发者_运维知识库</div> <div id=\"d2>Content</div>

I have a simple html page

<html>
<div id="d1>Content开发者_运维知识库</div>
<div id="d2>Content</div>
<div id="d3>Content</div>
<a href="page.html#d1">D1</a> <a href="page.html#d2">D2</a> <a href="page.html#d3">D3</a>
</html>

I want to highlight the selected div, with my script

<script>
var divID=window.location.href.split('#')[1];
$(divID).blahblahEffect;
<script>

The problem is this works only the first time when i moved from X page to page.html, after going to page.html if i click on any Links it does not show the effect.


try

CSS

.highlight{ background-color :#FCFC9F; }

jQuery

$("a").click(function (e) {
   $('div').removeClass('highlight');
   var hashId=this.hash.substr(1);
   $('#'+hashId).addClass('highlight');           
   e.preventDefault();
});

DEMO

0

精彩评论

暂无评论...
验证码 换一张
取 消