How to get all element parents using jquery? i want to save these parents in a variable so i can use later as a selector.
such as <div><a><img id="myImg"/></a></div>
GetParents('myImg'); will return "div a" something like that
如何使用jquery获取所有元素父项?我想将这些父节点保存在变量中,以便稍后可以用作选择器。例如
5 个解决方案
#1
/// Get an array of all the elements parents:
///获取父元素的所有元素的数组:
allParents = $("#myElement").parents("*")
/// Get the nested selector through an element's parents:
///通过元素的父项获取嵌套选择器:
function GetParents(id) {
var parents = $("#" + id).parents("*");
var selector = "";
for (var i = parents.length-1; i >= 0; i--) {
selector += parents[i].tagName + " ";
}
selector += "#" + id;
return selector;
}
GetParents('myImage') will return your nested selector: HTML BODY DIV A #myImage
GetParents('myImage')将返回您的嵌套选择器:HTML BODY DIV AmyImage
Note sure why you'd want this but its reuseable as a selector.
#2
You don't need to grab their selectors, as you can use them directly with jQuery afterwards.
您不需要抓取他们的选择器,因为您可以在之后使用jQuery直接使用它们。
If you want to use all parents later, you can do something like:
如果您想稍后使用所有父母,您可以执行以下操作:
var parents = $("#element").parents();
for(var i = 0; i < parents.length; i++){
$(parents[i]).dosomething();
}
#3
Every element has only one real parent. To access and save it, write the following:
每个元素只有一个真正的父元素。要访问和保存它,请写下以下内容:
myParent = $("#myElement").parent();
If you need the parents parents too, use .parents()
如果你也需要父母的父母,请使用.parents()
See documentation for further information:
有关详细信息,请参阅文档
#4
You can use parents() to get your immediate parent, and their parents on up the tree. you can also pass a selector as an arg to only get parents that match a certain criteria. For instance:
您可以使用parents()将您的直接父母和他们的父母放在树上。您还可以将选择器作为arg传递给仅获取符合特定条件的父级。例如:
$('#myelement').parents('[id$=container]')
to get all parents who have an id attribute whose value ends with the text "container"
获取具有id属性的所有父级,其值以文本“container”结尾
#5
You can get all the tags of an element's parents like this:
您可以像这样获取元素父项的所有标记:
var sParents = $('#myImg').parents('*').map(function() {
return this.tagName;
}).get().join(' ');
you can also replace this.tagName with this.id for example or other attributes
您也可以使用this.id或其他属性替换this.tagName
#1
/// Get an array of all the elements parents:
///获取父元素的所有元素的数组:
allParents = $("#myElement").parents("*")
/// Get the nested selector through an element's parents:
///通过元素的父项获取嵌套选择器:
function GetParents(id) {
var parents = $("#" + id).parents("*");
var selector = "";
for (var i = parents.length-1; i >= 0; i--) {
selector += parents[i].tagName + " ";
}
selector += "#" + id;
return selector;
}
GetParents('myImage') will return your nested selector: HTML BODY DIV A #myImage
GetParents('myImage')将返回您的嵌套选择器:HTML BODY DIV AmyImage
Note sure why you'd want this but its reuseable as a selector.
#2
You don't need to grab their selectors, as you can use them directly with jQuery afterwards.
您不需要抓取他们的选择器,因为您可以在之后使用jQuery直接使用它们。
If you want to use all parents later, you can do something like:
如果您想稍后使用所有父母,您可以执行以下操作:
var parents = $("#element").parents();
for(var i = 0; i < parents.length; i++){
$(parents[i]).dosomething();
}
#3
Every element has only one real parent. To access and save it, write the following:
每个元素只有一个真正的父元素。要访问和保存它,请写下以下内容:
myParent = $("#myElement").parent();
If you need the parents parents too, use .parents()
如果你也需要父母的父母,请使用.parents()
See documentation for further information:
有关详细信息,请参阅文档
#4
You can use parents() to get your immediate parent, and their parents on up the tree. you can also pass a selector as an arg to only get parents that match a certain criteria. For instance:
您可以使用parents()将您的直接父母和他们的父母放在树上。您还可以将选择器作为arg传递给仅获取符合特定条件的父级。例如:
$('#myelement').parents('[id$=container]')
to get all parents who have an id attribute whose value ends with the text "container"
获取具有id属性的所有父级,其值以文本“container”结尾
#5
You can get all the tags of an element's parents like this:
您可以像这样获取元素父项的所有标记:
var sParents = $('#myImg').parents('*').map(function() {
return this.tagName;
}).get().join(' ');
you can also replace this.tagName with this.id for example or other attributes
您也可以使用this.id或其他属性替换this.tagName