Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如何在我的网站的每个页面上重用html和导航栏?_Jquery_Html_Bootstrap 4_Navbar - Fatal编程技术网

Jquery 如何在我的网站的每个页面上重用html和导航栏?

Jquery 如何在我的网站的每个页面上重用html和导航栏?,jquery,html,bootstrap-4,navbar,Jquery,Html,Bootstrap 4,Navbar,我正在为我的网站使用Bootstrap4 每页包含相同的导航条形码。当我更改导航栏时,我必须将代码复制到每个页面 如何使用导航条代码创建一个文件,并在运行时将其包含在每个页面中,从而可以更新导航条一次,并在每个页面上显示相同的导航条 我知道如何在php中实现这一点,但我更喜欢使用javascript 我使用在其他网站上找到的示例制作了一个测试页面,但它不起作用,请参见在您的网站上,您在头部加载jquery,然后在页面底部加载jquery slim。在jquery中,不支持slim load()。

我正在为我的网站使用Bootstrap4

每页包含相同的导航条形码。当我更改导航栏时,我必须将代码复制到每个页面

如何使用导航条代码创建一个文件,并在运行时将其包含在每个页面中,从而可以更新导航条一次,并在每个页面上显示相同的导航条

我知道如何在php中实现这一点,但我更喜欢使用javascript


我使用在其他网站上找到的示例制作了一个测试页面,但它不起作用,请参见在您的网站上,您在头部加载jquery,然后在页面底部加载jquery slim。在jquery中,不支持slim load()。不要加载两个版本的jquery,而是在您的foot部分加载完整版本的jquery,并将jquery代码放在后面。以下是页面的外观:

<!DOCTYPE html>
<html lang="en">
<head>

</head>

<body>

<!--Navigation bar-->
<div id="nav-placeholder">

</div>


<!--end of Navigation bar-->


<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
$(function(){
  $("#nav-placeholder").load("navbar.html");
});
</script>
</body>

$(函数(){
$(“#导航占位符”).load(“navbar.html”);
});

在您的站点上,您在页面的头部加载jquery,然后在页面的底部加载jquery slim。在jquery中,不支持slim load()。不要加载两个版本的jquery,而是在您的foot部分加载完整版本的jquery,并将jquery代码放在后面。以下是页面的外观:

<!DOCTYPE html>
<html lang="en">
<head>

</head>

<body>

<!--Navigation bar-->
<div id="nav-placeholder">

</div>


<!--end of Navigation bar-->


<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
<script>
$(function(){
  $("#nav-placeholder").load("navbar.html");
});
</script>
</body>

$(函数(){
$(“#导航占位符”).load(“navbar.html”);
});

我找到了一个有效的解决方案

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
    $(function(){
        var filename = "https://www.vidalingua.com/navbar.html"
        $("#includedContent").load(filename);
    });
</script>
</head>
<body>
<div id="includedContent"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>

$(函数(){
变量文件名=”https://www.vidalingua.com/navbar.html"
$(“#includedContent”).load(文件名);
});

我找到了一个有效的解决方案

<!DOCTYPE html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
    $(function(){
        var filename = "https://www.vidalingua.com/navbar.html"
        $("#includedContent").load(filename);
    });
</script>
</head>
<body>
<div id="includedContent"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</body>

$(函数(){
变量文件名=”https://www.vidalingua.com/navbar.html"
$(“#includedContent”).load(文件名);
});

看一看,看一看,谢谢史蒂夫!我把你的代码复制到了,但是没有用。知道为什么吗?我的错crossorgin完整性编码有问题,请用以下代码替换jquery链接的脚本代码:
,它应该可以工作。我也会编辑答案。嘿,Steve,我用下面的行替换jQueryInclude,让你的代码正常工作了。code.jquery.com/jquery-1.10.2.js“>史蒂夫,你的更新也很有效。我忘了,谢谢!这项技术存在一个问题,那就是谷歌页面抓取。如果导航栏以这种方式包含,谷歌和其他机器人将无法找到其他页面。有人有解决办法吗?谢谢史蒂夫!我把你的代码复制到了,但是没有用。知道为什么吗?我的错crossorgin完整性编码有问题,请用以下代码替换jquery链接的脚本代码:
,它应该可以工作。我也会编辑答案。嘿,Steve,我用下面的行替换jQueryInclude,让你的代码正常工作了。code.jquery.com/jquery-1.10.2.js“>史蒂夫,你的更新也成功了。谢谢!这项技术存在一个问题,那就是谷歌页面爬网。如果导航栏以这种方式包含,谷歌和其他机器人将无法找到其他页面。有人对此有解决办法吗?