我看起来选择otf_itm类,但我在这个特定的页面中使用了两次.我需要使用标签作为选择otf_itm的根.我已经使用了以下工作正常但我想检查并确保它是否是在这种情况下使用的最佳实践.
JS:
$("label[for='town_id']").next().children();
Run Code Online (Sandbox Code Playgroud)
HTML:
<div class="form-group">
<label for="town_id">Town:</label>
<div class="col-md-3 controls">
<div class="otf_itm">
<!-- form items that chagne due to other data -->
</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
像这样使用
$("label[for='town_id']").next().find(".otf_itm");
Run Code Online (Sandbox Code Playgroud)
find()将搜索任何级别的子元素.同时children()将搜索第一级子元素.
在你的情况,无论是find()和children()做工精细.
或者你也可以这样使用,
$("label[for='town_id']").closest(".form-group").find(".otf_itm");
Run Code Online (Sandbox Code Playgroud)
closest() 将获得具有指定类的父级
| 归档时间: |
|
| 查看次数: |
36 次 |
| 最近记录: |