将 JQuery 的toggle() 与#anchors 混合
嘿,我有以下困境: 我有一组 DIV,其中包含默认隐藏的子 DIV。 最初,我使用 javascript 和带有锚点的 onclick 来实现切换和“移动到锚点”效果。 现在我已经转向 JQuery 替代方案,但在重现相同的“移动到锚点”效果时遇到了问题。 切换效果很好。
当您单击父 div 中的“h2 a”链接时,子 div 将通过切换显示。 下面是一个父级和子级 div 设置的示例:
<div class="full email">
<a id="test_anchor"></a>
<h2 class="subsubtitle"><a href="#test_anchor">TITLE AND LINK</a></h2>
<div class="description full">
<p>THIS IS WHAT SHOWS ALL THE TIME, REGARLDESS OF THE TOGGLE</p>
</div><!-- #description ends here -->
<div id="c_1">
THE DIV AND THE CONTENTS THAT ARE SHOWN AND HIDDEN
</div><!-- #c_1 div ends here -->
</div><!-- .full .email ends here -->
//And Here's the JQuery:
$(document).ready(function(){
$("#c_1,#c_2,#c_3,#c_4").hide();
$("div div h2 a").toggle(
function() { $(this).parent().siblings("div:last").slideDown("slow") },
function() { $(this).parent().siblings("div:last").hide() }
);
});
现在的问题是:如何激活或重新激活 #anchor,以便 Jquery slipDown/hide 功能和古老的“move-to-”锚'发挥作用?
亲切的问候 坎波斯
Hey there, I have the following dilemma:
I have a set of DIVs with child DIVS within them that are hidden by default. Initially, I was using javascript and onclick with anchors to achieve both a toggling and 'move-to-anchor' effect. Now that I have moved to the JQuery alternative, I am having problems reproducing the same 'move-to-anchor' effect. The toggling works just fine.
When you click an "h2 a" link in the parent div, the child div is shown through toggle. Here's a sample of one parent and child div setup:
<div class="full email">
<a id="test_anchor"></a>
<h2 class="subsubtitle"><a href="#test_anchor">TITLE AND LINK</a></h2>
<div class="description full">
<p>THIS IS WHAT SHOWS ALL THE TIME, REGARLDESS OF THE TOGGLE</p>
</div><!-- #description ends here -->
<div id="c_1">
THE DIV AND THE CONTENTS THAT ARE SHOWN AND HIDDEN
</div><!-- #c_1 div ends here -->
</div><!-- .full .email ends here -->
//And here's the JQuery:
$(document).ready(function(){
$("#c_1,#c_2,#c_3,#c_4").hide();
$("div div h2 a").toggle(
function() { $(this).parent().siblings("div:last").slideDown("slow") },
function() { $(this).parent().siblings("div:last").hide() }
);
});
Now the question is: How do I activate or reactivate that #anchor so that BOTH the Jquery slideDown/hide functions and the goold old 'move-to-anchor' come into play?
Kind regards
G.Campos
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用切换功能所做的是切换匹配元素的可见性,而不是它们内部的内容。
尝试这样的事情:
What you're doing with the toggle function is toggling visibility of the matched elements, not what's inside them.
Try something like this:
您是否想实现类似的事情?
相关 jQuery 代码
编辑:
为了回应您的评论,我在 这个示例 以及所有内容在 IE6 和 Firefox 3 中似乎都能正常工作。您可以通过在 URL 上添加
/edit
来编辑它的代码。 这个示例适合您吗?您使用什么浏览器?Were you trying to achieve something like this?
Relevant jQuery code
EDIT:
In response to your comment, I have put a number of the show/hide sections on this example and all appears to work fine for me in IE6 and Firefox 3. You can edit the code for it by adding
/edit
onto the URL. Does this example work ok for you and what browser are you using?