Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

Setting active section after redirect with jQuery

I'm building a personal one-page HTML template with different sections like about me, resume, portfolio, etc. The theme is using a navigation structure as followed:

<ul class="navbar-nav">
    <li class="nav-item active">
        <a class="nav-link" href="#About">About Me</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#Portfolio">Portfolio</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#Resume">Resume</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#Blog">Blog</a>
    </li>
    <li class="nav-item">
        <a class="nav-link" href="#Contact">Contact</a>
    </li>
</ul>

Needless to say, on each click on the menu items, the page moves to the respective section in the current document.

However, I decided to create a separate HTML file for blog posts named single-post.html which uses the exact same structure for navigation.

So, what I wanted to do next, was to redirect the user back to index.html and move them to the selected section when they clicked on any menu item. In other words, if the user is viewing single-post.html and clicks on <a class="nav-link" href="#Resume">Resume</a> they are moved to index.html or the main page of the theme, and immediately to the resume section.

This is how I tried to do that using the trigger() in jQuery:

$('.single .nav-link').click(function(event){
    event.preventDefault();
    
    var pageID = $(this).attr('href').split('#');
    var selectedPage = pageID[1];
    
    window.location.href = 'index.html';
    
    $('.nav-link[href="#'+ selectedPage +'"]').trigger('click');
});

Note: The class single is assigned to <body> of the single-post.html.

The code redirects the user to the index file eventually, but that selected section will not have active status.

I appreciate some help on this.

question from:https://stackoverflow.com/questions/65849064/setting-active-section-after-redirect-with-jquery

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't write code in post.html to be ran in index.html... And whatever you have after window.location.href = 'index.html'; won't be seen because the browser is loading the index.html.

But what you want to do is quite easy because you already have the anchors.

You know a click on the anchor <a class="nav-link" href="#Resume">Resume</a> is positionning the page to the element having the Resume id...

The same thing can be done by adding #Resume straight in the address bar when index.html is loaded.
???(If you didn't know... Try it).

So if you follow me...

In the post.html page, that will do exactly what you want:

window.location.href = 'index.html' + $(this).attr('href');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...