


Correct usage of previousSibling and nextSibling in javascript_javascript tips
When I do the time verification, the format does not need to be verified. I only need to verify the size of the start date and end date. However, because the input page is in batches and each row is automatically generated, it cannot be used. id is used as a parameter, and only nodes can be used. This makes verification more difficult.
The following is the part of the jsp page:
<td><input id="warrantyStartDateStr" name="warrantyStartDateStr" class="toolbar_button_input_80" type="Text" onClick="WdatePicker()"></td> <td><input id="warrantyEndDateStr" name="warrantyEndDateStr" class="toolbar_button_input_80" type="Text" onClick="WdatePicker()" onBlur="checkDateOne(this)"></td>
And my js function is finally written like this:
The purpose of the js function is to obtain the values of the two inputs, namely the start time and the end time, without passing the id, and then compare the two times.
function checkDateOne(inputsNode){ var p = inputsNode.parentNode; //取得input节点的父节点td var preNode=p.previousSibling.firstChild;//取得td节点的前一个兄弟节点的第一个子结点 var startDate = document.getElementByIdx_x(preNode.id).value; var endDate = document.getElementByIdx_x(inputsNode.id).value; if(startDate.length>0 && endDate.length>0){ var startTmp=startDate.split("-"); var endTmp=endDate.split("-"); var sd=new Date(startTmp[0],startTmp[1],startTmp[2]); var ed=new Date(endTmp[0],endTmp[1],endTmp[2]); if(sd.getDate()>=ed.getDate()){ alert("起始日期不能大于结束日期"); //return false; } } }
First, get the parent node p (i.e. td node) of the current node input node, and then get the first child node input of the previous node of the parent node. This achieves the goal.
What I want to emphasize here is that don’t forget that the td node is the parent node of the input node and cannot be regarded as its sibling node.
I would also like to say: the difference between previousSibling and nextSibling between IE and FF:
Let’s look at an example first:
<body> <div> <input id= "a4" type= "button" onclick= "alert(this.nextSibling);" value= "d" /> <input id= "a5" type= "button" onclick= "alert(this.nextSibling);" value= "e" /> </div> </body>
On the surface, the structure of this object shows that the nextSibling of the div has only two items - two input nodes. But there are actually 5 items -/n,input,/n,input,/n. This is because input is used as a label to create various form input controls. Whether it is generating buttons, checkboxes, radios... or other form controls, IE will automatically create a 1-byte blank at the end.
IE will skip the space document nodes (such as line feed characters) generated between nodes, but Mozilla will not do this - FF will treat layout elements such as space breaks and line feeds as node reads, so in The next node element that can be read using nextSibling in IE needs to be written like this in FF: nextSibling.nextSibling.
PreviousSibling has exactly the opposite effect, but the usage is the same!
Introduction to nextSibling and previousSibling
Contains many spaces as text nodes in FireFox, so problems will occur when we use nextSibling and previousSibling. Because FireFox will mistakenly treat text nodes as sibling nodes of element nodes. We can add nodeType to judge. When the previous node or next node is a text node, continue searching until the next element node is found. The following code is for reference only and passed the test in fireFox:
//下一个兄弟节点 function nextSibling(node) { var tempLast = node.parentNode.lastChild; if (node == tempLast) return null; var tempObj = node.nextSibling; while (tempObj.nodeType != 1 && tempObj.nextSibling != null) { tempObj = tempObj.nextSibling; } return (tempObj.nodeType==1)? tempObj:null; } //前一个兄弟节点 function prevSibling(node) { var tempFirst = node.parentNode.firstChild; if (node == tempFirst) return null; var tempObj = node.previousSibling; while (tempObj.nodeType != 1 && tempObj.previousSibling != null) { tempObj = tempObj.previousSibling; } return (tempObj.nodeType==1)? tempObj:null; } <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript" language="javascript" > window.onload = function () { var oUl = document.getElementsByTagName("UL"); var nodeLi = oUl[0].childNodes[3]; var nextListItem = nodeLi.nextSibling; var preListItem = nodeLi.previousSibling; alert(nextListItem.tagName + " " + preListItem.tagName); nextListItem = nextSibling(nodeLi); preListItem = prevSibling(nodeLi); alert(nextListItem.tagName + " " + preListItem.tagName); } //下一个兄弟节点 function nextSibling(node) { var tempLast = node.parentNode.lastChild; if (node == tempLast) return null; var tempObj = node.nextSibling; while (tempObj.nodeType != 1 && tempObj.nextSibling != null) { tempObj = tempObj.nextSibling; } return (tempObj.nodeType==1)? tempObj:null; } //前一个兄弟节点 function prevSibling(node) { var tempFirst = node.parentNode.firstChild; if (node == tempFirst) return null; var tempObj = node.previousSibling; while (tempObj.nodeType != 1 && tempObj.previousSibling != null) { tempObj = tempObj.previousSibling; } return (tempObj.nodeType==1)? tempObj:null; } </script> </head> <body> <form id="form1" runat="server"> <div> <ul> <li>HTML</li> <li>CSS</li> <li>JavaScript</li> <li>JQuery</li> <li>Dom</li> </ul> <ul> <li>ASP.NET</li> <li>JSP</li> <li>PHP</li> <li>VB.NET</li> </ul> </div> </form> </body> </html>
The main values of nodeType are as follows:
The nodeType value of the element node is 1
The nodeType value of the attribute node is 2
The nodeType value of the text node is 3

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

JavaScript can be run in PowerPoint, and can be implemented by calling external JavaScript files or embedding HTML files through VBA. 1. To use VBA to call JavaScript files, you need to enable macros and have VBA programming knowledge. 2. Embed HTML files containing JavaScript, which are simple and easy to use but are subject to security restrictions. Advantages include extended functions and flexibility, while disadvantages involve security, compatibility and complexity. In practice, attention should be paid to security, compatibility, performance and user experience.
