jQuery - 儿童的麻烦 [英] jQuery - Trouble with children

查看:135
本文介绍了jQuery - 儿童的麻烦的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个容器里面有几个div。只有一个div应该在一个时间。容器本质上只是一个jQuery的divs滑块。它应该显示和隐藏next / prev div取决于你点击哪个按钮,但它在下面这行返回一个错误:

I have a container with several divs inside of it. Only one of the divs should should at a time. The container is essentially just a jQuery slider of divs. It should show and hide the next/prev div depending on which button you click, but it returns an error on the following line:

children[current--].show();

我不确定为什么会发生这种情况。我调用孩子(一个数组),然后尝试访问索引与一个int--,然后显示它,但它从来没有显示。

I'm not sure as to why this is happening. I am calling children (an array) then trying to access the index with an int--, then showing it, but it never shows.

这里是一个jsFiddle: http:// jsfiddle。 net / n6fW7 / 1 /

Here is a jsFiddle: http://jsfiddle.net/n6fW7/1/

HTML:

<div id="container">
<div id="content">
    <div id="prev" class="arrow">></div>
    <div id="next" class="arrow"><</div>
</div>
</div>

jQuery:

$(function(){
    $('#content').append('<div class="box"><p class="title">1</p><p class="text">abc</p></div><div class="box"><p class="title">2</p><p class="text">efg</p></div>');

    var children = $('#content').find('.box');
    var count = children.length;
    var current = 1;
    $('.box').not(children[0]).hide();

    $('.arrow').click(function(){
        var where = $(this).attr('id');
        if(where == 'next'){
            if(current != count){
                current++;
            }
        }
        else{
            // previous
            if(current > 2){
                current--;
            }
        }
        $('.box').hide();
        children[current--].show();
    });
});


推荐答案

使用索引器在jQuery对象中返回原始DOM元素,它不包含 show()函数。您可以使用 eq()函数,而不是索引器,它返回一个包装DOM元素的jQuery对象:

Using the indexer in a jQuery object returns the raw DOM element, which does not contain the show() function. You can use the eq() function instead of the indexer, which returns a jQuery object wrapping the DOM element:

children.eq(current--).show();

这篇关于jQuery - 儿童的麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆