【发布时间】:2019-12-15 01:02:23
【问题描述】:
我尝试将网页的顶部栏分离到另一个 HTML 文件中。我能够使用 jQuery 'load' 函数将 HTML 文件加载到主网页中的 div 中,但问题是它没有正确显示。当我将所有代码放在同一个 HTML 文件中时,它看起来和我预期的一样。我制作了一个 div 以拥有calc(100% - N px),它在主 HTML 中正确显示。但是从单独文件加载的 HTML 不适合 div 宽度,尽管我设置了正文样式width: 100%。它只是在 div 中看起来像一个小得多的框。
目前,我没有看到我在代码上犯的任何错误。
HTML - index.html
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="jqueryjs.js"></script>
</head>
<body>
...
<div class = "TopBar" id = "TopBar"></div>
...
</body>
</html>
HTML - topbar.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="style.css" type="text/css">
</head>
<body style = "margin: 0px; height: 54px; width: 100%; background-color: #dddddd;">
<span class="helper"></span><h2>Drive</h2>
<button class="TopBarBtn"><img src="assets/icon_sort.png"> Name </button>
<button class="TopBarBtn"><img src="assets/icon_newFolder.png"> New Folder </button>
</body>
</html>
CSS
.TopBar{
position: fixed;
width: calc(100% - 251px);
height: 54px;
top: 138px;
left: 251px;
z-index: 5;
border-style: none;
background-color: #eeeeee;
overflow: hidden;
}
jQuery
$(document).ready(function(){
$("#TopBar").load('topbar.html');
});
【问题讨论】:
-
不要在 topbar.html 中使用 、、、 等,因为您已经在 index.html 中加载了它,全部加载到 index.html 中。 html
-
你的背景颜色太长了;你有六个 e 和一个 s;十六进制颜色的最大值为七个字符。