Javascript 无法将活动类添加到nav元素,因为nav是从另一个.html文件导入的
这是我第一次尝试实现HTML/CSS网站,所以请考虑这一点Javascript 无法将活动类添加到nav元素,因为nav是从另一个.html文件导入的,javascript,html,jquery,css,Javascript,Html,Jquery,Css,这是我第一次尝试实现HTML/CSS网站,所以请考虑这一点 body{ display: block; margin: 0; } .menu li{ display: inline-block; margin: 0; } ul.topnav{ background-color: #333; margin: 0; padding: 0; text-align:center; overflow: hidden; } u
我有多个.html页面,它们使用jQuery函数(我想是吧?)从一个nav.html实现navbar。其他html类似于索引。 你知道怎么了吗body{ display: block; margin: 0; } .menu li{ display: inline-block; margin: 0; } ul.topnav{ background-color: #333; margin: 0; padding: 0; text-align:center; overflow: hidden; } ul.topnav li a{ display: block; font-size: 20px; padding: 10px; color: whitesmoke; text-decoration: none; } ul.topnav li a:hover:not(.active) {background-color: #222;} ul.topnav li a.active {background-color: #4CAF50;} @media screen and (max-width: "600px") { ul.topnav li {float: none;} } #brand{ display: table-cell; vertical-align: middle; line-height:50px; } #topText{ max-width: 500px; margin: auto; text-align: center; border: 5px solid green; color: red; background-color: #0f0f0f; margin-top: 10px; } .firstPage { margin-top: -20px; background-color:grey; max-width: 100%; height: auto; } h4{ padding: 20px; text-align: center; } .center { display: block; margin-left: auto; margin-right: auto; width: 50%; } #pageContent{ text-align:center; width: 75%; margin: auto; color: blue; }
<script> $(function(){ $("#nav-placeholder").load("nav.html"); }); </script>
使用jquery on ready函数包装代码,并移至页面底部$(函数(){ const currentLocation=location.href; const menuItem=document.querySelectorAll('a'); const menulelength=menuItem.length for(设i=0;i
})没关系,我用,而不是$(窗口)解决了这个问题。在“加载”上,是function(){
。简单的解释是,一旦整个页面(图像或iFrame),而不仅仅是DOM准备好,代码就会运行。->/p>出了什么问题?我没有看到问题描述,只有“出了什么问题”,是否出现控制台错误?是否发生意外情况?包装javscript代码$(文档)。ready(function(){
const currentLocation=location.href;const menuItem=document.querySelectorAll('a');const menulelength=menuItem.length for(让i=0;i您正在加载内容之前添加活动类。jQuery
在DOM加载后执行。我刚刚在标记$(document)之前添加了它。ready(function(){const currentLocation=location.href;const menuItem=document.querySelectorAll('a');const menulelength=menuItem.length for(设i=0;i$(function(){…})
body{ display: block; margin: 0; } .menu li{ display: inline-block; margin: 0; } ul.topnav{ background-color: #333; margin: 0; padding: 0; text-align:center; overflow: hidden; } ul.topnav li a{ display: block; font-size: 20px; padding: 10px; color: whitesmoke; text-decoration: none; } ul.topnav li a:hover:not(.active) {background-color: #222;} ul.topnav li a.active {background-color: #4CAF50;} @media screen and (max-width: "600px") { ul.topnav li {float: none;} } #brand{ display: table-cell; vertical-align: middle; line-height:50px; } #topText{ max-width: 500px; margin: auto; text-align: center; border: 5px solid green; color: red; background-color: #0f0f0f; margin-top: 10px; } .firstPage { margin-top: -20px; background-color:grey; max-width: 100%; height: auto; } h4{ padding: 20px; text-align: center; } .center { display: block; margin-left: auto; margin-right: auto; width: 50%; } #pageContent{ text-align:center; width: 75%; margin: auto; color: blue; }
$(function () { const currentLocation = location.href; const menuItem = document.querySelectorAll('a'); const menuLength = menuItem.length for (let i = 0; i < menuLength; i++) { if (menuItem[i].href === currentLocation) { menuItem[i].className = "active"; } }