开发者

jquery expand collapsed when coming from another page

I do not think this is possible开发者_开发问答 but just want to be sure. I am trying to expand a collapsed div (with jquery) when coming from another page. For example: from the home page a user clicks on view press release and it goes to the page with many press releases already collapsed. I want the correct press released expanded. Is this even possible?


It is definitely possible - assuming you control the target page. In that case, you can add an extra argument to your request link specifying the press release tho expand (for example, mypage.html?press_release_id=1234)

And then, in the target page (the one with the press releases), and using getParameterByName() from How can I get query string values in JavaScript?, you can easily parse this argument and expand the corresponding div:

$(document).ready(
   function() {
      var id = getParameterByName("press_release_id");
      $("#" + id).show();
   });


On the document ready function expand the ones you want.

$(document).ready(function(){
 //expand collapse here
});


What about looking at the document.referrer? If the url contains the information that would refer to the specific div, then just trigger the event that would expand it in your ready handler.


var hash = window.location.hash;
$('div'+hash+':first').show();

... to be used with something like: page.html#press-release-4

and a DIV having press-release-4 as ID of course


you could use querystring to specify which press release to show.

here's how to parse the querystring: How can I get query string values in JavaScript?

assuming you use the function from the aforementioned link it would be something like:

on home:

<a href="press.html?pr=monkeysattack">Monkeys attack small children.</a>

on press.html:

$(function() { $('.pr').not('# + getParameterByName('pr')).hide(); }


  1. Shows the Collapse DIV and won't remove/hide it

     $(document).ready(function() {
       $(window.location.hash).show();
     }); 
    
  2. Shows the Collapse DIV and collapses it after clicking on another collapse button/link within same page

     $(document).ready(function() {
       $(window.location.hash).addClass('collapse in');
     }); 
    
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜