1

I have a page where you can load content via JavaScript from an onclick function. Is it possible to link to the page where it automatically loads one of the functions?

HTML:

<nav>   
            <p id="bout" onclick="bout()">About Us</p>
            <p id="mish" onclick="mish()">Our Mission</p>
            <p id="team" onclick="team()">The Team</p>
            <p id="how" onclick="how()">How It Works</p>
            <p id="poli" onclick="poli()">Policies</p>
        </nav>

        <div class="actual">

            <div id="about">
            <h2>About Us</h2>
            <p>We are a conglomerate of hoodlums.</p>
            </div>

 </div><!-- end actual -->

JS:

    function bout() {
        document.getElementById("about").innerHTML= '<h2>About Us</h2><p>We are a conglomerate of hoodlums.</p>';
    }
function mish() {
    document.getElementById("about").innerHTML = '<h2>Mission</h2><p>Our mission is to rid the world of dust bunnies.</p>';

I would like to be able to link directly to this page with mish() loaded instead of the default HTML. Is this possible?

4

2 回答 2

4

是的,使用哈希标签并从那里调用函数。

哈希 URL 如下所示:

http://myserver.com/mypage.htm#about

创建一个加载页面时调用正确函数的js函数,例如:

function onloaded(){
    hash = document.location.hash;
    switch (hash):
        case 'mish':
        mish();
        break;
    ...
}
于 2013-07-18T19:30:05.310 回答
0

您正在寻找一个 url 哈希来维护状态。

例如:

function bout() {
     document.getElementById("about").innerHTML= '<h2>About Us</h2><p>We are a conglomerate of hoodlums.</p>';    
     document.location.hash="bout";
}

然后在页面加载的时候,查看document.location.hash的值,如果等于“bout”,就调用bout函数。

于 2013-07-18T19:30:18.753 回答