jQuery if url hash, click event/activate javascript

前端 未结 3 1488
悲哀的现实
悲哀的现实 2021-01-01 01:11

So from the home page, i have a link that goes to a products listing page. The product page has expand/collapse divs.

I need the appropriate div to expand depending

3条回答
  •  误落风尘
    2021-01-01 01:32

    You can make the following changes in the document ready function of your product page:

    Simple fix: Since the jQuery id-selector is #elementId, you can simply use the window.location.hash value as your id selector, and use it to target the desired element.

    if ( window.location.hash ) {
        $(window.location.hash).click(); //clicks on element specified by hash
    }
    

    Better: In addition to the above, take the js out of your markup.

    $('#healthysnacks').click(function(e) {
        e.preventDefault();
        ReverseDisplay('products4');
    });
    

    Then, after doing this, use the $(window.location.hash).click() code from above. Also, change your link to:

     Healthy Snacks
    

提交回复
热议问题