JQuery,隐藏在 Firefox 中不起作用
我目前正在为我的大学的开源研究项目构建一个非常简单的网站。 我正在使用 JQuery 为网站的子导航设置动画。 但是,我的代码似乎只能在 IE 中运行,而不能在 Firefox 或 Chrome 中运行。
我不确定这是兼容性问题还是我的问题。 我在网上查找了示例,但我没有发现代码有任何差异,无法解释为什么我的代码不起作用。
该网站部分的 HTML 如下:
<!-- START NAVIGATION & SUB NAVIGATION -->
<div class="nav">
<ul>
<li><a class="nav_home" href='#'><span>home</span></a></li>
<li><a class="nav_about" href="#"><span>about</span></a></li>
<li><a><span>research</span></a></li>
<li><a><span>findings</span></a></li>
<li><a><span>contact</span></a></li>
</ul>
</div>
<div class="sub_nav">
<ul class="sub_nav_home">
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
</ul>
<ul class="sub_nav_about">
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
</ul>
</div>
<!-- FINISH NAVIGATION -->
**注意:这只是测试信息,以确保我可以在实现实际内容之前使导航正常工作。 此外,只有前两个链接有效。 在我开始工作之前,我认为没有必要将它们全部实现。
JavaScript 如下:
var current_sub = 0;
$(document).ready(function() {
//hide elements
$("div.sub_nav > ul").hide();
function get_sub_navigation(nav_name)
{
if(current_sub != 0)
{
$(current_sub).fadeOut("slow", function ()
{
$(nav_name).slideDown("slow");
});
}
else
{
$(nav_name).slideDown("slow");
}
current_sub = nav_name;
}
$("a.nav_home").click(function(event)
{
event.preventDefault();
get_sub_navigation("ul.sub_nav_home");
}
);
$("a.nav_about").click(function(event)
{
event.preventDefault();
get_sub_navigation("ul.sub_nav_about");
}
);
});
好的,第一件事就是隐藏所有子导航列表。 然后我有一个监听器,用于监听两个应该调用 get_sub_navigation 的导航链接。 该函数将检查是否有显示(仅使用 0 表示默认/无),如果有隐藏它,如果没有,则显示一个。
正如您所看到的,它不是完成的代码,但是,在我弄清楚这一点之前,我不想进一步移动。
任何帮助深表感谢!
I am currently working on building a very simple site for an open source research project for my University. I am using JQuery to animate the sub-navigation of the site. However, my code only seems to work in IE and not Firefox or Chrome.
I'm not sure if it is a compatibility issue, or if it is my fault. I looked up examples on the web, and I do not see any differences in code as to why mine will not work.
The HTML for the section of the site is as follows :
<!-- START NAVIGATION & SUB NAVIGATION -->
<div class="nav">
<ul>
<li><a class="nav_home" href='#'><span>home</span></a></li>
<li><a class="nav_about" href="#"><span>about</span></a></li>
<li><a><span>research</span></a></li>
<li><a><span>findings</span></a></li>
<li><a><span>contact</span></a></li>
</ul>
</div>
<div class="sub_nav">
<ul class="sub_nav_home">
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
</ul>
<ul class="sub_nav_about">
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
<li><a><span>sub link</span></a></li>
</ul>
</div>
<!-- FINISH NAVIGATION -->
**Note: this is just testing information to make sure I can get the navigation working before implementing the real thing. Also, only the first two links work. I didn't see the need to implement them all until I got it working.
And the JavaScript is as follows :
var current_sub = 0;
$(document).ready(function() {
//hide elements
$("div.sub_nav > ul").hide();
function get_sub_navigation(nav_name)
{
if(current_sub != 0)
{
$(current_sub).fadeOut("slow", function ()
{
$(nav_name).slideDown("slow");
});
}
else
{
$(nav_name).slideDown("slow");
}
current_sub = nav_name;
}
$("a.nav_home").click(function(event)
{
event.preventDefault();
get_sub_navigation("ul.sub_nav_home");
}
);
$("a.nav_about").click(function(event)
{
event.preventDefault();
get_sub_navigation("ul.sub_nav_about");
}
);
});
Ok, so the first thing is does is hide all sub nav lists. Then I have a listener for two of the nav links that should call get_sub_navigation
. That function will check if there is one showing (just used 0 for default/nothing) and if there is hide it and if not, then show one.
As you can see, it is not finished code but, I don't want move any further until I can get this figured out.
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
调用
event.preventDefault()
在您的事件处理程序中,而不是在导航函数中的$(nav_name).preventDefault()
(或将事件传递到其中)。 我怀疑默认设置没有被阻止,并且页面正在被重新绘制。Call
event.preventDefault()
in your event handler, not$(nav_name).preventDefault()
in your navigation function (or pass the event into it). I suspect that the default is not getting prevented, and the page is being redrawn.,要在页面加载时默认隐藏 ul 元素,您可以简单地使用 css 而不是 javascript:(
尽管我不是 100% IE <= 6.0,但使用此选择器将 100% OK)
首先 我绝不是 jQuery 专家,我想既然选择器返回多个元素,它就需要不同的方法。
尝试
First of all, to hide the ul elements by default on page load you can simply use css instead of javascript:
(i'm not 100% IE <= 6.0 will be 100% OK with this selector, though)
Second, and while i am by no means a jQuery expert, i would imagine that since the selector returns multiple elements it needs a different approach.
try
好的,我已经在 Firefox 和 Chrome 中显示了 ul。 问题是您在定义它之前调用了 current_sub 。 这会导致它失败。 我认为这应该解决它。
Alright, I got the ul's to show in Firefox and chrome. The problem was that you called current_sub before defining it. That would cause it to fail. I think this should take care of it.