jQuery 使用商店选择器查找子元素

jQuery using store selector to find child elements

我经常在 foreach 循环中使用 this 关键字来 select this

的子元素
$('.width', this).val();

但是我可以对存储的变量做同样的事情吗?类似于:

var window = $('#window');
$('.width', window).val();

我想存储 $('#window'),因为它在我的代码中经常使用。我以为我可以使用上面的方法,但它对我不起作用。

更新 我正在寻找的相关元素是:

<div id="window">
    <input class="width" type="text" />
</div>

语法

$(child, inContext)

相同
$(inContext).find(child)

是的,你可以做到

var window = $('#window');
window.find('width').val();

并注意 width 寻找像 <width value="xxx"></width> 这样的元素,这看起来很奇怪