jQuery:相同标记名的同级数
这里,我带来了一个jQuery问题。考虑这个随机元素:jQuery:相同标记名的同级数,jquery,Jquery,这里,我带来了一个jQuery问题。考虑这个随机元素: <div id="some_random_id"> <b></b> <div></div> <b></b> <div></div> <div></div> </div> 当对象为DIV标记名时,函数必须返回2,当标记名为B时,函数必须返回1。我们如何做到这一点?谢谢这将选择具有
<div id="some_random_id">
<b></b>
<div></div>
<b></b>
<div></div>
<div></div>
</div>
当对象为DIV标记名时,函数必须返回2,当标记名为B时,函数必须返回1。我们如何做到这一点?谢谢这将选择具有相同标签的父母的所有子女:
function n_siblings_same_tagname(element){
element = $(element);
return element.parent().children(element.prop("tagName")).length;
};
这将选择具有相同标记的父项的所有子项:
function n_siblings_same_tagname(element){
element = $(element);
return element.parent().children(element.prop("tagName")).length;
};
我建议:
function n_siblings_same_tagname(node){
// Retrieve the tagName of the passed-in
// DOM node:
var elType = node.tagName;
// Converting the children the passed-node's
// parent's element children into an Array,
// using Array.from():
return Array.from( node.parentNode.children )
// filtering that array, using an Arrow
// which retains only those elements
// whose tagName is equal to the
// tagName of the passed-in elementary:
.filter( elem => elem.tagName == elType )
// finding the length of the filtered Array, and
// subtracting 1, as we want siblings not all
// elements of that type:
.length - 1;
}
或者,如果首选jQuery:
function n_siblings_same_tagname(element){
// we may receive a jQuery object, or a DOM
// node; here we wrap the received element
// with jQuery:
var el = $(element);
// returning the length of the number of
// sibling elements matching the
// supplied selector, here the tagName
// of the passed in element, and
return el.siblings(el.prop('tagName')).length
}
鉴于OP的回应,在下面的评论中,我将提供以下备选方案:
function n_siblings_same_tagname(element){
// we may receive a jQuery object, or a DOM
// node; here we wrap the received element
// with jQuery:
var el = $(element);
// returning the length of the number of
// child elements matching the selector
// provided by the tagName property
// of the passed-in element:
return el.parent().children(el.prop('tagName')).length
}
或者,修改后的JavaScript:
function n_siblings_same_tagname(node){
// Retrieve the tagName of the passed-in
// DOM node:
var elType = node.tagName;
// Converting the children the passed-node's
// parent's element children into an Array,
// using Array.from():
return Array.from( node.parentNode.children )
// filtering that array, using an Arrow
// which retains only those elements
// whose tagName is equal to the
// tagName of the passed-in elementary:
.filter( elem => elem.tagName == elType )
// finding the length of the filtered Array:
.length;
}
我建议:
function n_siblings_same_tagname(node){
// Retrieve the tagName of the passed-in
// DOM node:
var elType = node.tagName;
// Converting the children the passed-node's
// parent's element children into an Array,
// using Array.from():
return Array.from( node.parentNode.children )
// filtering that array, using an Arrow
// which retains only those elements
// whose tagName is equal to the
// tagName of the passed-in elementary:
.filter( elem => elem.tagName == elType )
// finding the length of the filtered Array, and
// subtracting 1, as we want siblings not all
// elements of that type:
.length - 1;
}
或者,如果首选jQuery:
function n_siblings_same_tagname(element){
// we may receive a jQuery object, or a DOM
// node; here we wrap the received element
// with jQuery:
var el = $(element);
// returning the length of the number of
// sibling elements matching the
// supplied selector, here the tagName
// of the passed in element, and
return el.siblings(el.prop('tagName')).length
}
鉴于OP的回应,在下面的评论中,我将提供以下备选方案:
function n_siblings_same_tagname(element){
// we may receive a jQuery object, or a DOM
// node; here we wrap the received element
// with jQuery:
var el = $(element);
// returning the length of the number of
// child elements matching the selector
// provided by the tagName property
// of the passed-in element:
return el.parent().children(el.prop('tagName')).length
}
或者,修改后的JavaScript:
function n_siblings_same_tagname(node){
// Retrieve the tagName of the passed-in
// DOM node:
var elType = node.tagName;
// Converting the children the passed-node's
// parent's element children into an Array,
// using Array.from():
return Array.from( node.parentNode.children )
// filtering that array, using an Arrow
// which retains only those elements
// whose tagName is equal to the
// tagName of the passed-in elementary:
.filter( elem => elem.tagName == elType )
// finding the length of the filtered Array:
.length;
}
id为'some_random_id'的div中的其他div是子级,而不是同级。你能改变标题来反映这一点吗?是的,没错。我们对父元素一无所知。我们只知道一个指向子元素的指针,其他子元素是该元素的同级元素,我们知道如果我们返回这些数字,那么您不会检索具有给定标记名的同级元素的数量,而是查找具有该标记名的所有子元素,同级元素加上原始元素。请你把你的问题具体说明一下你想要什么?刚刚编辑过。我想要的是与给定元素相同标记的所有元素的数量。仅编辑为兄弟姐妹,因为它更容易理解,并且获取答案的方式不会有太大变化。id为'some_random_id'的div中的其他div是子级,而不是兄弟姐妹。你能改变标题来反映这一点吗?是的,没错。我们对父元素一无所知。我们只知道一个指向子元素的指针,其他子元素是该元素的同级元素,我们知道如果我们返回这些数字,那么您不会检索具有给定标记名的同级元素的数量,而是查找具有该标记名的所有子元素,同级元素加上原始元素。请你把你的问题具体说明一下你想要什么?刚刚编辑过。我想要的是与给定元素相同标记的所有元素的数量。仅编辑为兄弟,因为它更容易理解,并且获取答案的方式没有太大变化。这假设
此
是一个jQuery对象而不是domeElement,但在这种情况下可以工作。您只需要返回length
属性。第一行用于什么?正如rory所说,该元素不是jquery对象。如果您确定只使用jQuery对象调用它,则可以删除该行。这假定this
是jQuery对象而不是domeElement,但在这种情况下可以工作。您只需要返回length
属性。第一行用于什么?正如rory所说,该元素不是jquery对象。如果您确定只使用jQuery对象调用它,那么可以删除lineMy Error,我的意思是元素本身及其所有同级。无论如何,答案的逻辑变化不大。谢谢你回答我的错误,我的意思是元素本身和它的所有兄弟。无论如何,答案的逻辑变化不大。谢谢你的回答