Home Web Front-end JS Tutorial js jquery gets the sibling level of the current element previous next element_jquery

js jquery gets the sibling level of the current element previous next element_jquery

May 16, 2016 pm 03:41 PM

var chils= s.childNodes; //Get all child nodes of s

var par=s.parentNode; //Get the parent node of s

var ns=s.nextSbiling; //Get the next sibling node of s

var ps=s.previousSbiling; //Get the previous sibling node of s

var fc=s.firstChild; //Get the first child node of s

var lc=s.lastChile; //Get the last child node of s

JS gets node parent, child element

Let’s first talk about the JS acquisition method, which is much more troublesome than the JQUERY method. The JQUERY method will be used for comparison later.

The JS method is much more troublesome than JQUERY, mainly because FF browser will treat your line breaks as DOM elements

<div id="test">
<div></div>
<div></div>
</div>
Copy after login

Native JS gets the child elements under the element with ID test.

Can be used:

var a = docuemnt.getElementById("test").getElementsByTagName_r("div"); 
Copy after login

This is no problem

At this time a.length=2;

But if we use another method

var b =document.getElementByIdx_x("test").childNodes; 
Copy after login

At this time, b.length is no problem in IE browser, it is still equal to 2, but in FF browser, it will be 4, because FF also treats line breaks as an element.

So, here, we are going to do processing. We need to traverse these elements and delete all elements that are spaces and text.

function del_ff(elem){
var elem_child = elem.childNodes;
for(var i=0; i<elem_child.length;i++){
if(elem_child[i].nodeName == "#text" && !/\s/.test(elem_child.nodeValue))
{elem.removeChild(elem_child)
}
}
}
Copy after login

The above function traverses the child elements. When there is a node type in the element that is text and the node value of the text type node is empty. Just delete him.

nodeNames can get the node type of a node, /s/ is the regular expression of non-empty characters in JS. Add in front! , it means the empty character

The test() method is used to check whether a string matches a certain pattern. The syntax is: RegExpObject.test(string)

Returns true if the string string contains text matching RegExpObject, otherwise returns false.

nodeValue means getting the value in this node.

removeChild deletes the child elements of the element.

After that, before calling the properties of son, father, brother, just call the above function to clear the spaces

<div id="test">
<div></div>
<div></div>
</div>

<script>
function dom() {
var s= document.getElementByIdx_x("test");
del_ff(s);  //清理空格
var chils= s.childNodes; //得到s的全部子节点
var par=s.parentNode;  //得到s的父节点
var ns=s.nextSbiling;  //获得s的下一个兄弟节点
var ps=s.previousSbiling; //得到s的上一个兄弟节点
var fc=s.firstChild;  //获得s的第一个子节点
var lc=s.lastChile;  //获得s的最后一个子节点
}
</script>

Copy after login

The following introduces JQUERY’s method of finding parent, child, and sibling nodes

jQuery.parent(expr) To find the parent node, you can pass in expr for filtering, such as $("span").parent() or $("span").parent(".class")

jQuery.parents(expr), similar to jQuery.parents(expr), but searches all ancestor elements, not limited to parent elements

jQuery.children(expr). Returns all child nodes. This method will only return direct child nodes and will not return all descendant nodes

jQuery.contents(), returns all content below, including nodes and text. The difference between this method and children() is that including blank text will also be treated as a

jQuery object is returned, children() will only return nodes

jQuery.prev(), returns the previous sibling node, not all sibling nodes

jQuery.prevAll(), returns all previous sibling nodes

jQuery.next(), returns the next sibling node, not all sibling nodes

jQuery.nextAll(), returns all subsequent sibling nodes

jQuery.siblings(), returns sibling nodes, regardless of front or rear

jQuery.find(expr) is completely different from jQuery.filter(expr). jQuery.filter() filters out a part of the initial collection of jQuery objects, while jQuery.find()
The return result will not contain the content in the initial collection. For example, $("p"), find("span") starts from the

element to find , which is equivalent to $("p span" )

The above introduction is about js jquery getting the sibling level of the current element. Previous Next element. I hope it will be helpful to everyone.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Replace String Characters in JavaScript Replace String Characters in JavaScript Mar 11, 2025 am 12:07 AM

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

Custom Google Search API Setup Tutorial Custom Google Search API Setup Tutorial Mar 04, 2025 am 01:06 AM

This tutorial shows you how to integrate a custom Google Search API into your blog or website, offering a more refined search experience than standard WordPress theme search functions. It's surprisingly easy! You'll be able to restrict searches to y

Build Your Own AJAX Web Applications Build Your Own AJAX Web Applications Mar 09, 2025 am 12:11 AM

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

Example Colors JSON File Example Colors JSON File Mar 03, 2025 am 12:35 AM

This article series was rewritten in mid 2017 with up-to-date information and fresh examples. In this JSON example, we will look at how we can store simple values in a file using JSON format. Using the key-value pair notation, we can store any kind

8 Stunning jQuery Page Layout Plugins 8 Stunning jQuery Page Layout Plugins Mar 06, 2025 am 12:48 AM

Leverage jQuery for Effortless Web Page Layouts: 8 Essential Plugins jQuery simplifies web page layout significantly. This article highlights eight powerful jQuery plugins that streamline the process, particularly useful for manual website creation

What is 'this' in JavaScript? What is 'this' in JavaScript? Mar 04, 2025 am 01:15 AM

Core points This in JavaScript usually refers to an object that "owns" the method, but it depends on how the function is called. When there is no current object, this refers to the global object. In a web browser, it is represented by window. When calling a function, this maintains the global object; but when calling an object constructor or any of its methods, this refers to an instance of the object. You can change the context of this using methods such as call(), apply(), and bind(). These methods call the function using the given this value and parameters. JavaScript is an excellent programming language. A few years ago, this sentence was

Improve Your jQuery Knowledge with the Source Viewer Improve Your jQuery Knowledge with the Source Viewer Mar 05, 2025 am 12:54 AM

jQuery is a great JavaScript framework. However, as with any library, sometimes it’s necessary to get under the hood to discover what’s going on. Perhaps it’s because you’re tracing a bug or are just curious about how jQuery achieves a particular UI

10 Mobile Cheat Sheets for Mobile Development 10 Mobile Cheat Sheets for Mobile Development Mar 05, 2025 am 12:43 AM

This post compiles helpful cheat sheets, reference guides, quick recipes, and code snippets for Android, Blackberry, and iPhone app development. No developer should be without them! Touch Gesture Reference Guide (PDF) A valuable resource for desig

See all articles