【问题标题】:cannot access child element - jquery无法访问子元素 - jquery
【发布时间】:2010-06-27 15:25:41
【问题描述】:

我有一个类似的 HTML

<div class="a">
    <div class="b">
        something
    </div>

    <div class="c">
        <div class="subC">
            i want to access
        </div>
    </div>
</div>

和 jquery 一样

$('.a').hover(function(){
    $(this).children('.subC').fadeOut();
})

我想访问“subC”类,但上面不起作用。

我也试过

$('.a').hover(function(){
    $(this).children('.c .subC').fadeOut();
})

但这也行不通!

这个问题的解决方案是什么!我做错什么了吗?请帮忙

【问题讨论】:

    标签: jquery html function hover children


    【解决方案1】:

    children 只深一层。请改用find()

    http://api.jquery.com/children/

    【讨论】:

      【解决方案2】:

      当在 jQuery 闭包中时,this 指的是之前 jQuery 操作返回的 jQuery 对象:

      $('.a').hover(function() {
          // 'this' is a jQuery object containing the result of $('.a')
      })
      

      在闭包中使用this来设置当前jQuery对象内部的查询范围:

      $('.a').hover(function() {
          $('.subC', this).fadeOut();
      })
      

      【讨论】:

        【解决方案3】:

        使用.find('selector') 寻找深度孩子

        【讨论】:

          【解决方案4】:

          正如 Rob 所说,使用 .find 查找深层元素。

          $('.a').hover(function()
              {
                  $(this).find('.c .subC').fadeOut();
              });
          

          如果你想使用.children,请写

          $('.a').hover(function(){
              $(this).children('.c').children('.subC').fadeOut();
          })
          

          【讨论】:

            猜你喜欢
            • 2012-12-21
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-07-31
            • 2011-12-10
            • 1970-01-01
            • 1970-01-01
            • 2011-08-06
            相关资源
            最近更新 更多