Home > Web Front-end > JS Tutorial > body text

Correct usage of previousSibling and nextSibling in javascript_javascript tips

WBOY
Release: 2016-05-16 15:39:13
Original
1175 people have browsed it

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>
Copy after login

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;   
  }   
  }
 }
Copy after login

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> 
Copy after login

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)&#63; 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)&#63; 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)&#63; 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)&#63; 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>
Copy after login

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

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template