


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

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

Both Python and JavaScript's choices in development environments are important. 1) Python's development environment includes PyCharm, JupyterNotebook and Anaconda, which are suitable for data science and rapid prototyping. 2) The development environment of JavaScript includes Node.js, VSCode and Webpack, which are suitable for front-end and back-end development. Choosing the right tools according to project needs can improve development efficiency and project success rate.

Yes, the engine core of JavaScript is written in C. 1) The C language provides efficient performance and underlying control, which is suitable for the development of JavaScript engine. 2) Taking the V8 engine as an example, its core is written in C, combining the efficiency and object-oriented characteristics of C. 3) The working principle of the JavaScript engine includes parsing, compiling and execution, and the C language plays a key role in these processes.

JavaScript is at the heart of modern websites because it enhances the interactivity and dynamicity of web pages. 1) It allows to change content without refreshing the page, 2) manipulate web pages through DOMAPI, 3) support complex interactive effects such as animation and drag-and-drop, 4) optimize performance and best practices to improve user experience.

C and JavaScript achieve interoperability through WebAssembly. 1) C code is compiled into WebAssembly module and introduced into JavaScript environment to enhance computing power. 2) In game development, C handles physics engines and graphics rendering, and JavaScript is responsible for game logic and user interface.


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

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

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

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
