如何将 var/id 与类匹配
我是 jquery 的新手,此外,我认为我的大脑冻结了。
我有许多具有不同 ID 的链接。我想将单击的链接与具有相应类的 div 相匹配,以便 div 将根据需要显示/隐藏/切换。
我有:
<script type="text/javascript">
$(document).ready(function() {
$('.folioBox').hide();
$('a.interface').click(function(){
var currentId = $(this).attr('id');
var currentBox = $('.folioBox .' + currentId);
$(currentBox).toggle(400);
});
});
</script>
<a class="interface" id="apple">Apple flavor</a>
<a class="interface" id="banana">Banana flavor</a>
<a class="interface" id="cherry">Cherry flavor</a>
<div class="folioBox apple">content</div>
<div class="folioBox banana">content</div>
<div class="folioBox cherry">content</div>
我只是无法让它工作,而且我不知道我是否最好使用匹配、查找或过滤。
一些帮助将不胜感激!
I'm new to jquery and, in addition, I think I'm having a brain freeze.
I have a number of links with different ids. I want to match the clicked link with a div with the corresponding class so the div will show/hide/toggle as appropriate.
I have:
<script type="text/javascript">
$(document).ready(function() {
$('.folioBox').hide();
$('a.interface').click(function(){
var currentId = $(this).attr('id');
var currentBox = $('.folioBox .' + currentId);
$(currentBox).toggle(400);
});
});
</script>
<a class="interface" id="apple">Apple flavor</a>
<a class="interface" id="banana">Banana flavor</a>
<a class="interface" id="cherry">Cherry flavor</a>
<div class="folioBox apple">content</div>
<div class="folioBox banana">content</div>
<div class="folioBox cherry">content</div>
I just can't get it to work and I can't figure out whether I would be best using match or find or filter.
Some assistance would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试:
没有必要让它变得更复杂。如果有多个匹配项,那么它们都会被切换。
注意:我已使点击处理程序
返回 false
,以便停止跟踪链接。根据href
属性中的内容,页面实际上可能会刷新,这解释了为什么它看起来不起作用。Try:
No need to make it more complex than that. If there are multiple matches, then they'll all be toggled.
Note: I've made the click handler
return false
so stop the link being followed. Depending on what you have in thehref
attribute, the page may actually being refreshed, which explains why it appears not to be working.现在只是看看它,但你是否尝试过在最后因为撞到锚点而返回 false ?
让我知道这是否有效,我将停止调试此端。
编辑
去掉两个类之间的空格。
Just looking at it now but have you tried doing a return false at the end because you are hitting an anchor?
let me know if that works and i'll stop debugging this end.
EDIT
Get rid of the space between the two classes.