Jquery toggle()、hide() 未按预期工作
我刚刚开始在 Drupal 环境中使用 JQuery。
我有一些缩略图,我希望当您单击其中一个时,更大的图片会出现在“视口”中(出现一个隐藏的 div)。可能有更好的方法来完成我正在做的事情,但我需要这样做。
这就是我所拥有的:
$(document).ready(function() {
//$('#bigpic1').hide(); //show the first big picture always on page load
$('#bigpic2').hide();
$('#bigpic3').hide();
$('a#thumb1').click(function() {
$('#bigpic2').hide(); $('#bigpic3').hide();
$('#bigpic3').toggle(200);
return false;
});
$('a#thumb2').click(function() {
$('#bigpic1').hide(); $('#bigpic3').hide();
$('#bigpic2').toggle(200);
return false;
});
$('a#thumb3').click(function() {
$('#bigpic1').hide(); $('#bigpic2').hide();
$('#bigpic3').toggle(200);
return false;
});
});
除了一些丑陋的代码之外,它也不能正常工作。当页面出现时,第一张大图片不会出现,单击更多缩略图会显示正确的 div - 但永远不会隐藏任何内容(“视口”中一次应该只显示一张大图片)。
我的 HTML 看起来像这样
<table><tr>
<td><a href="#" mce_href="#" id="thumb1">...thumb1...</td>
<td><a href="#" mce_href="#" id="thumb2">...thumb2...</td>
<td><a href="#" mce_href="#" id="thumb3">...thumb3...</td>
</tr>
<tr><td colspan="3">
<!-- my "viewport" cell -->
<!-- the big picture displays here -->
<div id="bigpic1">... </div>
<div id="bigpic2">...</div>
<div id="bigpic3">...</div>
</td></tr></table>
I'm just starting out with JQuery in a Drupal environment.
I have some thumbnails, and I want it so that when you click one, the bigger picture appears in the "viewport" (a hidden div appears). There might be better ways to accomplish what I am doing, but I need to do it this way.
This is what I have :
$(document).ready(function() {
//$('#bigpic1').hide(); //show the first big picture always on page load
$('#bigpic2').hide();
$('#bigpic3').hide();
$('a#thumb1').click(function() {
$('#bigpic2').hide(); $('#bigpic3').hide();
$('#bigpic3').toggle(200);
return false;
});
$('a#thumb2').click(function() {
$('#bigpic1').hide(); $('#bigpic3').hide();
$('#bigpic2').toggle(200);
return false;
});
$('a#thumb3').click(function() {
$('#bigpic1').hide(); $('#bigpic2').hide();
$('#bigpic3').toggle(200);
return false;
});
});
Besides being some ugly code, it doesn't work right. The first big picture doesn't appear when the page does, and clicking on more thumbnails shows the right div - but never hides any (only one big picture is supposed to be visible at a time in the "viewport").
My HTML looks like this
<table><tr>
<td><a href="#" mce_href="#" id="thumb1">...thumb1...</td>
<td><a href="#" mce_href="#" id="thumb2">...thumb2...</td>
<td><a href="#" mce_href="#" id="thumb3">...thumb3...</td>
</tr>
<tr><td colspan="3">
<!-- my "viewport" cell -->
<!-- the big picture displays here -->
<div id="bigpic1">... </div>
<div id="bigpic2">...</div>
<div id="bigpic3">...</div>
</td></tr></table>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
# 选择器选择一个 id,因此您不需要其中的类型。选择器是 jQuery 最好的功能之一,因此请更加熟悉它们。 http://api.jquery.com/category/selectors/。
您甚至可以执行这样的通用方法(您需要为 元素命名)。
以及 jQuery 代码。
这是更少的代码,更具可扩展性。如果您添加/删除图像,则不需要更改它。
关于加载时显示的第一张图像,您是否在文档加载时尝试过
$('#bigpic1').show();
。哦,您不需要在函数中返回值。The # selector selects an id so you don't need the type in there. Selectors are on of jQuery's best features, so get more familiar with them all. http://api.jquery.com/category/selectors/.
You could even do a general method like this (you need to give the <a> elements a name).
And the jQuery code.
This is less code, that is more extensible. It will not need to be changed if you add/remove images.
About thie first image showing on load did you try
$('#bigpic1').show();
on document load. Oh and you don't need to return a value in the functions.首先,通过 ID 或类来识别“视口单元格”会很方便。我不知道文档的其余部分是如何设置的,但是例如:
同样,也许您可以向缩略图链接添加一个类以使事情变得更容易:
设置要隐藏的“bigpics”的默认样式:
最后,使用 jQuery:
这是一个示例: http://jsfiddle.net/redler/SDxwF/
First, it would be convenient to identify your "viewport cell" by ID or class. I don't know how the rest of your document is set up, but for example:
Likewise, perhaps you can add a class to your thumbnail links to make things easier:
Set the default style for your "bigpics" to be hidden:
Finally, with jQuery:
Here's an example: http://jsfiddle.net/redler/SDxwF/