[英]How to get the children of specific parent? JQuery
var subtypechecked = function() { var subtype = $(this); var evidence = subtype.parent().parent().parent().children("input[name^='Regression.']").fadeOut(); alert(evidence.context.name); }; $("input[type=checkbox]").on("click", subtypechecked);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <div class="row"> <div class="panel panel-default"></div> <div class="panel-heading clearfix"></div> <div style="padding-left:15px;padding-top:5px"></div> <div class="panel-body" style="padding-top:5px;padding-bottom:5px"> <label class="checkbox-inline"> <input type="checkbox" value="1" name="Repression">Repression </label> <label class="checkbox-inline"> <label class="checkbox-inline"> <label class="checkbox-inline"> </div> <div class="panel-body" style="padding-top:5px;padding-bottom:5px"> <label class="checkbox-inline"> <input type="checkbox" value="1" name="Repression.Confirmed">Repression.Confirmed </label> <label class="checkbox-inline"> <input type="checkbox" value="2" name="Unknown.Confirmed">Unknown.Confirmed </label> <label class="checkbox-inline"> <label class="checkbox-inline"> </div> </div>
我想獲取所有名稱以“ Repression”開頭的元素。 但是,我檢查了螢火蟲。 “證據”是我單擊的按鈕。 那沒有道理。
http://api.jquery.com/children/
.children()方法與.find()的不同之處在於,.children()僅沿DOM樹向下移動一個級別,而.find()可以向下遍歷多個級別以選擇后代元素(孫子等)。
您正在執行.children
在你的.row
格,只。孩子着眼於直接desendants,你是想用.find將看在DOM樹元素更深。
var evidence = subtype.parent()
.parent()
.parent()
.find("input[name^='Repression.']").fadeOut();
另外您正在使用壓制而不是名稱回歸
您的問題與context
。 引用jQuery文檔,上下文為:
DOM節點上下文最初傳遞給jQuery(); 如果沒有通過,那么上下文很可能是文檔。
因此它將返回傳遞給jQuery的原始DOM,即單擊的復選框,因為選擇器找不到與查詢匹配的元素。
您可以使用parents
find
:
var subtypechecked = function() {
var subtype = $(this);
var evidence = subtype.parents('.row')
.find("input[name^='Repression.']");
alert(evidence.attr('name'));
// fadeOut the parent label to hide the text, as requested
evidence.parent().fadeOut();
};
$("input[type=checkbox]").on("click", subtypechecked);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.