简体   繁体   English

我的代码有什么问题?

[英]What is wrong with my code?

I have 4 links, when each link is clicked, I am taking the index of that link. 我有4个链接,当单击每个链接时,我正在获取该链接的索引。 I am getting index + 1 = 1, 2, 3 4 like this. 我正在像这样获得index + 1 = 1, 2, 3 4 According to me when user click on the link, the indexed 'li' need to fadeIn, reset have to fadeOut. 据我说,当用户单击链接时,索引为“ li”的需要淡入,重新设置必须为淡出。 so, whatever the link is clicked according to the link index + 1 have to fadeIn, rest should not in the view. 因此,无论是根据链接索引+1来单击链接,还是必须在fadeIn中单击,其余都不应出现在视图中。 for this I wrote this code, but not working. 为此,我编写了这段代码,但是没有用。 what is wrong with my code ? 我的代码有什么问题?

$('#b-fmg-slider a').each(function(index){
    $(this).click(function(e){
        $('#b-fmg-slider a').removeAttr('class');
        $('#b-fmg-slider ul li').fadeOut();
         $('#b-fmg-slider ul li',':nth-child('+index+1+')').fadeIn();
    })    
})

can any one say, whether what I am doing here is wrong? 谁能说我在这里做错了吗?

thanks in advance. 提前致谢。

This is can be achieved very easily using an alternative approach. 使用替代方法可以很容易地实现这一点。 First fadeout all the li-s (using a class selector), then fadeIn $(this) (the item which was clicked on). 首先淡出所有li-s(使用类选择器),然后淡入$(this) (被单击的项目)。

As to why your code is wrong, you are using two separate selectors (if you separate them with a comma, they are separate selectors). 至于为什么代码错误,则使用两个单独的选择器(如果用逗号将它们分开,则它们是单独的选择器)。

Try something like this: 尝试这样的事情:

$('#b-fmg-slider a').click(function (e) {
  $('#b-fmg-slider ul li').fadeOut();
  $(this).parent('li').next('li').fadeIn();
});

Fading out all the li-s and then finding the li which you clicked on (if its not a direct descendant, use parents() instead of parent() ), then find the one after it - to fade him in. 淡出所有li-s,然后找到您单击的li(如果它不是直接后代,请使用parent()代替parent() ),然后找到其后的一个-使他淡入。

When you pass in a second parameter, you're actually giving jQuery a context to work on 当您传入第二个参数时,实际上是给jQuery一个上下文以供处理

$('#b-fmg-slider ul li',':nth-child('+index+1+')').fadeIn();//does not work

Just use the index and a filter 只需使用索引和过滤器

var i = $(this).index();
$('#b-fmg-slider ul li:eq(' + i + ')').fadeIn();

Here's a simple fiddle : http://jsfiddle.net/WU7Tt/ 这是一个简单的小提琴: http : //jsfiddle.net/WU7Tt/

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

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