简体   繁体   English

jQuery中的选择器

[英]selector in jquery

I am having some problems with the sub nav on this site I am building. 我正在建立的这个网站上的潜水艇导航出现问题。 I dont think I am selecting items properly in my jquery. 我不认为我在我的jquery中正确选择项目。 Im sure its just a little thing that needs to be done, but I dont know how to do it. 我确定这只是一件小事,但是我不知道该怎么做。

http://jsfiddle.net/ZDErp/ http://jsfiddle.net/ZDErp/

I am trying to make it so when you click on one of the subnav links a different div will open up revealing a color. 我试图做到这一点,所以当您单击子导航链接之一时,将打开另一个div,以显示一种颜色。 For some reason when I click on a link it only reveals the first(red) div. 由于某些原因,当我单击链接时,它仅显示第一个(红色)div。

If you can help that would be great! 如果您能提供帮助,那就太好了!

The problem is with this line: 问题在于此行:

var $menuelement = $('.thumb').eq($(this).parent().index());

a.subnav 's direct parent is an h4 . a.subnav的直接父母是h4 Using index() on this element would mean you want the index of that h4 in relation to its siblings (there are none). 在此元素上使用index()意味着您想要该h4与其同级(没有)的索引。 You actually want the closest li ancestor. 你真正想要的最接近 li的祖先。

Use: 采用:

var $menuelement = $('.thumb').eq($(this).closest("li").index());

Updated fiddle: http://jsfiddle.net/286LV/ 更新的小提琴: http : //jsfiddle.net/286LV/

I updated the fiddle: http://jsfiddle.net/ZDErp/1/ 我更新了小提琴: http : //jsfiddle.net/ZDErp/1/

What I did was us the href attribute as the selector for the div to show. 我所做的是将href属性用作div显示的选择器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM