【问题标题】:How would I write a jQuery selector to grab the child <input> of $(this)?我将如何编写一个 jQuery 选择器来获取 $(this) 的子 <input>?
【发布时间】:2012-12-22 20:02:02
【问题描述】:

我试过了:

$(this).nextAll("input").get(0)

它不仅不起作用,而且如果它起作用,它看起来会有点矫枉过正。

【问题讨论】:

  • 有人能解释一下为什么我的这个简单问题被否决了吗?得到两个答案、对这些答案的 9 个赞成票和一个最喜欢的答案就足够了。

标签: jquery jquery-selectors


【解决方案1】:
//child
$(this).children('input');

//descendant
$(this).find('input');
$('input', this);

顺便说一句,.nextAll 是给兄弟姐妹的。

【讨论】:

    【解决方案2】:

    简单地使用这个:

    $('input', this)
    

    如果你需要 DOM 元素而不需要 jQuery 包装的元素,请使用

    $('input', this).get(0)
    

    nextAll 不是在寻找孩子,而是在寻找兄弟姐妹。这就是您的代码无法正常工作的原因。

    【讨论】:

      猜你喜欢
      • 2010-09-23
      • 1970-01-01
      • 2010-10-24
      • 1970-01-01
      • 2012-04-18
      • 1970-01-01
      • 2015-12-06
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多