首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$.children()和$.children('className')有什么区别?

$.children()和$.children('className')有什么区别?
EN

Stack Overflow用户
提问于 2013-12-09 04:35:22
回答 2查看 75关注 0票数 0

我有两个班如下:

- css:

代码语言:javascript
复制
.shw-intro {
    width: 250px;
    height: 150px;
    background: rgb(95, 190, 0);
    position: absolute;
    top: 25px;
    left: 50px;
}
.shw-intro-content {
    display: none;
}

-- html:

代码语言:javascript
复制
<div class="shw-intro">
    <div class="shw-intro-content">hello</div>
    <div class="shw-intro-content">everyone</div>
    <div class="shw-intro-content">have fun</div>
 </div>

在javascript中,我尝试制作了一些动画,但在两种情况下我都感到惊讶:

代码语言:javascript
复制
var currentShw = 0;

情况1:

代码语言:javascript
复制
 function shwAnimation() {
            if ($('.shw-intro').attr('class').indexOf('shw-intro-translate') != -1) {
                $('.shw-intro').removeClass('shw-intro-translate');
                currentShw = currentShw >= $('.shw-intro-content').length ? 0 : currentShw + 1;
            }
            else {
                $('.shw-intro').children().eq(currentShw).show(); // It works fine
                $('.shw-intro').addClass('shw-intro-translate');
            }
        }

情况2:

代码语言:javascript
复制
function shwAnimation() {
        if ($('.shw-intro').attr('class').indexOf('shw-intro-translate') != -1) {
            $('.shw-intro').removeClass('shw-intro-translate');
            currentShw = currentShw >= $('.shw-intro-content').length ? 0 : currentShw + 1;
        }
        else {
            $('.shw-intro').children('shw-intro-content').eq(currentShw).show(); // It doesn't work
            $('.shw-intro').addClass('shw-intro-translate');
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2013-12-09 04:44:33

您应该使用CSS选择器作为.children()的参数。

像这样:$('.shw-intro').children('.shw-intro-content')

票数 2
EN

Stack Overflow用户

发布于 2013-12-09 04:46:02

这应该能行

代码语言:javascript
复制
function shwAnimation() {
 if ($('.shw-intro').attr('class').indexOf('shw-intro-translate') != -1) {
     $('.shw-intro').removeClass('shw-intro-translate');
   currentShw = currentShw >= $('.shw-intro-content').length ? 0 : currentShw + 1;   }
    else {
        $('.shw-intro').children('.shw-intro-content').eq(currentShw).show(); 
        $('.shw-intro').addClass('shw-intro-translate');
    }
}

必须通过使用“”来使用类选择器。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20463235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档