用户单击导航栏中的项目后,如何在div容器中加载html页面?

时间:2018-11-08 17:07:40

标签: javascript jquery html twitter-bootstrap

我有main.html页面,其顶部具有导航栏菜单。一共有三个不同的项目HomeContactAbout。用户首次点击应用程序www.example/main.html的链接Home页面时,这将是首次加载,因为这是默认的活动链接。我想看看用户是否单击AboutContact来加载特定的容器,然后更改活动链接CSS,最后在URL栏中仅显示没有文件扩展名的页面名称。像这样的www.example/about。我在项目中使用JQuery和Bootstrap 3。这是我当前代码的示例:

$(document).ready(function(){
	  $("#home").load("includes/home.html");
});
.navbar-brand {
    padding: 0px;
}
.navbar-brand>img {
    height: 100%;
    min-height: 45px;
    padding: 5px;
    width: auto;
}
.jumbotron {
    padding-bottom: 10px;
}
.page-footer {
    background-color: #eee; 
    border-radius: 6px;
    padding: 5px;
}
.footer-txt {
    font-size: 10px;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Main Page</title>
    <link rel="icon" type="image/x-icon" href="images/favicon.ico"/>
    <link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
    <nav id="myNavbar" class="navbar navbar-default navbar-inverse navbar-fixed-top" role="navigation">
        <div class="container">
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#navbarCollapse">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
                <a class="navbar-brand" data-toggle="tooltip" data-placement="top" title="Main Page" href="https://www.example.com" target="_blank"><img class="img-rounded" src="images/logo.png" alt="Main"></a>
            </div>
            <div class="collapse navbar-collapse" id="navbarCollapse">
                <ul class="nav navbar-nav">
                    <li class="active"><a href="/home" data-id="home" target="_blank">Home</a></li>
                    <li><a href="/about" data-id="about" target="_blank">About</a></li>
                    <li><a href="/contact" data-id="contact" target="_blank">Contact</a></li>
                </ul>
            </div>
        </div>
    </nav>
    <div class="container">
        <div class="jumbotron">
            <h2>Main Page</h2>
        </div>
        <div id="home"></div>
        <div id="about"></div>
        <div id="contact"></div>
        <hr>
        <div class="row">
            <div class="col-sm-12">
                <footer class="page-footer">
                    <div class="container-fluid">
                         <div class="row">
                         <div class="col-xs-12 col-sm-12 col-md-2 col-lg-2">
                             <p class="text-center">&copy; Copyright 2018</p>
                         </div>
                         </div>
                    </div>
                </footer>
            </div>
        </div>
    </div>
    <script language="javascript" type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="crossorigin="anonymous"></script>
    <script language="javascript" type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script></script>
</body>
</html>

如果有人知道如何实现这一目标,请告诉我。

0 个答案:

没有答案