javascript JQuery查找父div子标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25767774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery find parent div child label
提问by Steve
When i click on expandable-icons-cancelI want to save a child label of form-groupdiv. Here is the structure:
当我单击expandable-icons-cancel 时,我想保存表单组div的子标签。这是结构:
<div class="form-group ">
<label class="" style="padding-top:10px;"> Long Title</label>
<span class="value ">
<div class="expandable">
<div class="expandable-icons">
<img class="expandable-icons-cancel" style="display:none;" src="test/cancel.png">
</div>
</div>
</span>
</div>
And function:
和功能:
jQuery('.expandable-icons-cancel').click(function() {
var parentDiv = jQuery(this).parents('div.expandable'); //-works, but how can i get upper?
// How to get 2 steps upper to form-group?
// How to get get child label text of form-group and save in var?
}
回答by Dallas
You can use closest()
, like this
你可以closest()
像这样使用
jQuery(this).closest(".form-group");
for the label text you can do...
对于标签文本,您可以执行...
jQuery(this).closest(".form-group").find("label").text();
if you know label will always be a direct child of .form-group
, you can use children()
in place of find()
. If there is a possible for multiple labels and you only want to get the first, you can place eq(0)
after getting the label.
如果您知道 label 始终是 的直接子代.form-group
,则可以使用children()
代替find()
。如果有多个标签的可能,而你只想得到第一个,你可以eq(0)
在得到标签后放置。