This article mainly introduces the methods for obtaining the prev and next of an element's adjacent sibling nodes in JavaScript, involving JavaScript function-based judgment and techniques for calling previussibling and nextSibling, for more information about how to obtain the prev and next methods of an element, see the example in this article. We will share this with you for your reference. The details are as follows:
/*** Obtain the adjacent element * @ param ele reference object element * @ param type, the previous one (1) or the next one (0) * @ return returns the searched element Dom object. If none, null is returned */function getNearEle (ele, type) {type = 1? "Previussibling": "nextSibling"; var nearEle = ele [type]; while (nearEle) {if (nearEle. nodeType = 1) {return nearEle;} nearEle = nearEle [type]; if (! NearEle) {break;} return null;}/*** get the previous element of the current execution object */function prev () {return getNearEle (this, 1 );} /*** get the next element of the current execution object */function next () {return getNearEle (this, 0);} // var ele = document. getElementById ("xxx"); // var prevElement = prev. call (ele); // var nextElement = next. call (ele );