jquery hide() 函数
我正在尝试使用 jQuery 创建滑动常见问题解答菜单,但收到一条错误,指出 hide() 为 null。我已经在 Mac 上的 Chrome 和 Firefox 以及 Win 上的 Firefox 中尝试过。这是代码:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.hide').hide();
$('h5').click(function(){
$(this).next().slideToggle("slow");
return false;
});
});
这是 HTML:
<a href="#" ><h5 >The Heading </h5></a><br />
<div class="hide">
The content.
</div>
我花了过去三个小时翻阅在线书籍和教程,一切看起来都正确,但显然不是。 :P
非常感谢任何帮助。
I'm trying to create a sliding FAQ menu using jQuery, but I'm getting an error that states the the hide() is null. I've tried it in Chrome and Firefox on Mac and Firefox on Win. Here's the code:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.hide').hide();
$('h5').click(function(){
$(this).next().slideToggle("slow");
return false;
});
});
And here is the HTML:
<a href="#" ><h5 >The Heading </h5></a><br />
<div class="hide">
The content.
</div>
I've spent the last three hours pouring over books and tutorials online and everything looks right, but apparently not. :P
Any help is much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的
.click()
处理程序位于上;
元素,从那里它试图隐藏.next()
< /a> 同级 元素...这些都没有。而不是这样:
您需要类似
.nextAll()
(在上搜索所有以下同级:
您可以在这里尝试一下。
或者,也许更干净一点,您可以向锚点添加一个类来简化事情,如下所示:
然后,由于您位于
上,因此不需要
.parent()
调用,如下所示:您可以在此处尝试该版本。
Your
.click()
handler is on the<h5>
element, from there it's trying to hide the.next()
sibling element...there aren't any of those.Instead of this:
You'd need something like
.nextAll()
(that searches all following siblings) on the<a>
:You can try it out here.
Alternatively, maybe a bit cleaner, you can add a class to the anchor to simplify things, like this:
Then, since you're on the
<a>
, you don't need the.parent()
call, like this:You can try that version out here.
更新:在 Ant 发布该页面的公共 URL 后,我可以自信地重申我的主张,即 Ant 在其原始问题中发布的示例代码没有问题,所以感谢那些对我投反对票的人。问题是 Ant 的页面同时使用原型库和 jQuery 库。原始问题中省略了这一事实。这两个库在 $ 变量上存在冲突。如果您将代码 Ant 更改为使用关键字 jQuery 而不是 $ ,
我相信您的代码将有效。或者,您可以使用 jQuery 页面上描述的无冲突技术
http://api.jquery.com/ jQuery.noConflict/
您的代码没有任何问题。您可以将其放在公共网站上并分享 URL 吗?我唯一能想到的是 jQuery 没有被引入。
尝试将
HEAD 标签中的
和其余 javascript 代码移到结束
标签之后
Update: After Ant published the public URL of the page I can confidently reiterate my claim that there is no problem in the sample code posted by Ant in his original question so thank you whoever down voted me. The problem is that Ant's page is using both prototype and jQuery libraries together. This fact is omitted from the original question. These two libraries are conflicting over $ variable. If you change your code Ant to use the keyword jQuery instead of $ everywhere as
I am confident your code will work. Alternatively you can use the no conflict technique as described on jQuery pages here
http://api.jquery.com/jQuery.noConflict/
There is nothing wrong with your code. Can you put this on a public site and share the URL? The only thing I can think of is that jQuery is not being pulled in.
Try moving the
in the HEAD tag
and the rest of the javascript code after the closing
tag
我已经制作了一个小而简单的示例来说明如何执行此操作(DEMO)。我认为这可能比您当前的方法更干净一些。我从来不喜欢使用
作为标题。
HTML
JS
CSS
I have whipped up a small, simple example of how to do this (DEMO). I think this may be a little cleaner than your current method. I never liked using
<a>
as the header.HTML
JS
CSS