当前位置:网站首页>JS find all nodes sibling childNodes children
JS find all nodes sibling childNodes children
2022-07-27 19:51:00 【HandsomeDanielWu】
siblings Brothers and sisters





childNodes Unique child nodes 
Place the space <div/> Text node <p><p/><div>
children




Find all nodes 







![]()




encapsulation , Return all element sibling nodes
<body>
<div class="box">
<p> I'm a paragraph </p>
<p> I'm a paragraph </p>
<p> I'm a paragraph </p>
<p id="fpara"> I'm a paragraph </p>
I am the text
<!-- I am a comment -->
<p id="para">
I'm a paragraph para
<span>1</span>
<span>2</span>
<span>3</span>
</p>
<p> I'm a paragraph </p>
<p> I'm a paragraph </p>
<p> I'm a paragraph </p>
</div>
<script>
var box = document.getElementById("box");
var para = document.getElementById("para");
var fpara = document.getElementById("fpara");
// encapsulation , Return all element sibling nodes
function getAllElementSiblings(node){
// Brother node in front
var prevs = [];
// Brother nodes behind
var nexts = [];
var o = node;
// facilitate Node The front node
while(o.previousSibling != null){
if(o.previousSibling.nodeType == 1){
prevs.unshift(o.previousSibling);
}
o = o.previousSibling ;
}
o = node;
// facilitate Node Back node
while(o.nextSibling != null){
if(o.nextSibling.nodeType == 1){
nexts.push(o.nextSibling);
}
o = o.nextSibling;
}
//prevs Array merge nexts Array
return prevs.concat(nexts);
}
console.log(getAllElementSiblings(para));
</script>
</body>边栏推荐
猜你喜欢

RadioGroup(单选框)

Map和Set

redis底层数据结构详解

5W bonus pool / for colleges and universities, 2022 legal science and technology innovation competition is in progress

A low code development platform that brings high-value user experience
![[basic knowledge of deep learning - 42] detailed explanation of logistic regression](/img/74/d7d1562ada4671864961721b9a1baf.png)
[basic knowledge of deep learning - 42] detailed explanation of logistic regression

Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased

嵌入式C语言结构体

Low code implementation exploration (45) business parameters

High cost, difficult to implement, slow to take effect, what about open source security?
随机推荐
[basic knowledge of deep learning - 50] PCA dimensionality reduction principal component analysis
【深度学习基础知识 - 41】深度学习快速入门学习资料
JS 事件监听 鼠标 键盘 表单 页面 onclick onkeydown onChange
Influxdb series (IV) TSM engine (storage principle)
[deep learning target detection series - 01] what is target detection
RadioGroup(单选框)
【深度学习基础知识 - 39】BN、LN、WN的比较
MySQL time zone problem
BroadcastReceiver(广播)
Unity-FairyGUI播放视频(Lua)
【深度学习基础知识 - 42】逻辑回归详解
【深度学习基础知识 - 50】PCA降维 主成成分分析
【深度学习目标检测系列 - 01】目标检测是什么
Intent(有无返回值得跳转)
go-zero单体服务使用泛型简化注册Handler路由
Flink 算子简介
Combinatorics -- permutation and combination
【深度学习基础知识 - 44】逻辑回归实现多分类的方法
[basic knowledge of deep learning - 44] the method of realizing multiple classification by logistic regression
Install Talib Library under Anaconda