页脚上的 javascript 驱动的导航菜单
我的标题上的导航菜单如下所示:
<ul id="nav">
<li id="home">
<a class="mainmenu" href="#">Link1</a>
</li>
<li>
<a class="mainmenu" href="#">Link2</a>
</li>
</ul>
相同的标记用于页脚部分,但它不起作用。 我还有一个名为 jscript.js 的文件,其中包含该网站的所有 javascript, 我发现了这个变量:
var navTarget = "ul#nav li a" //menu link to target
此外,如果我删除例如标题部分中的标记,页脚将起作用。 我也尝试过使用 .nav 而不是 #nav,我也遇到了同样的问题。
导航菜单是由javascript控制的,我不在这里发布代码,因为它很大,为了更好地理解导航菜单的工作原理,请查看这里
我在javascript中找到了这个:
//SET MENU ITEM IDs
$(navTarget).each(function(i){
i++
this.id = this.id +"_" +i ;
});
//MENU CLICK FUNCTION
$(navTarget).click(function() {
//ensure link isnt clickable when active
if ($(this).hasClass('active')) return false;
//get id of clicked item
activeNavItem = $(this).attr('id');
//call the page switch function
switchContent();
});
//CONTENT SWTICH FUNCTION
var switchContent = function (){
//set previous and next link & page ids
var PrevLink = $(navTarget+'.active')
$(PrevLink).removeClass('active');
var PrevId = $(PrevLink).attr('id');
//alert(PrevId)
var NextLink = $('#'+activeNavItem).addClass('active');
var NextId = activeNavItem
//alert(NextId);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从表面上看,JS 代码正在使用一些 CSS 选择器(如 jquery 的 $ 或 dojo 的 dojo.query),它根据
navTarget
的值提取 DOM 元素目标,然后执行以下操作it:将其变成菜单。但它只做一次。
你需要看一下JS,看看哪里使用了navTarget。然后,让它在
$(navTarget)
的所有结果上创建菜单应该相当容易,而不仅仅是第一次点击。另外,你的 dom 中应该只有一个 ID 实例。
您可以将其更改为类:
在标记中:
但是您仍然需要查看 JS 并确保它针对 CSS 选择器返回的一组目标起作用。该代码可能只期望一个结果并仅使用它:
results[0]
。From the looks of it, the JS code is using some CSS selector (like jquery's $ or dojo's dojo.query) that pulls in the DOM element target based on the value of
navTarget
, and then does something with it: turns it into a menu.But its only doing it once.
You need to look at the JS and see where navTarget is used. Then it should be fairly easy to make it do the menu creation on all the results of
$(navTarget)
instead of just the first hit.Also, you should only have on instance of an ID in your dom.
You can change this to a class instead:
And in the markup:
But you will still have to look at the JS and make sure it functions against a set of targets returned by the CSS selector. That code is probably expecting just a single result and using just it:
results[0]
.页面上只能有一个给定 id 的元素。因此,根据您的描述,听起来您有 2 个。
我不知道这个脚本到底是如何工作的,但您可以尝试使用类。
您必须更改 HTML 和 JS
navTarget
选择器字符串。但您的脚本也很可能根本不支持创建多个菜单。如果是这种情况,您可能需要修复该脚本或找到更好的脚本。
You can only have one element of a given id on the page. So based on your description, it sounds like you have 2.
I don't know exactly how this script works, but you can try using classes instead.
You would have to change your HTML and the JS
navTarget
selector string.But there is also a good chance that your script may not support creating multiple menus at all. And if thats the case, you may need to fix that script or find a better one.
如果页脚的代码确实与页眉相同,那就是问题所在。 id 只能用于页面中的单个元素,并且 jQuery 的选择器只会返回第一个元素。像“ul#nav li a”这样的含义代码仅适用于标题。
最简单的解决方案是将 id 更改为类,例如:
... 并更改 jQuery 以匹配它,例如:
更新: 并且(忽略这可能最终会变成三个重复的帖子),修复可能根本不够,因为脚本的其他部分可能只适用于单个菜单。
If the code for the footer really is identical to the header, that's the problem. An id should only be used for a single element in a page, and jQuery's selectors will only return the first. Meaning code like "ul#nav li a" only works on the header.
Easiest solution is to change the id's to classes, e.g.:
... and change your jQuery to match that, e.g.:
Update: And (ignoring that this may end up turning into three duplicate posts), that fix is probably not enough at all, since other parts of the script may only work with a single menu.