如何在不带库的Vanilla JavaScript中实现“ prevUntil”?

问题描述 投票:11回答:7

我需要在Vanilla JavaScript中实现jQuery的prevUntil()方法的功能。

我在同一级别上有几个prevUntil()元素:

<div>

我正在尝试使用<div></div> <div></div> <div></div> <div></div> <div></div> <div></div> 事件查找onclickevent.target 直到]达到特定条件(例如,类名匹配),然后停止。

我该如何实现?

我需要在Vanilla JavaScript中实现jQuery的prevUntil()方法的功能。我在同一级别上有几个

元素:
&...
] >>

此答案先前是针对类似问题发表的previousSiblings

有几种方法。

以下任何一项都可以解决问题。

here

FYI:jQuery代码库是观察A级Javascript的绝佳资源。

这里是一个非常出色的工具,可以非常简化地显示jQuery代码库。// METHOD A (ARRAY.FILTER, STRING.INDEXOF) var siblings = function(node, children) { siblingList = children.filter(function(val) { return [node].indexOf(val) != -1; }); return siblingList; } // METHOD B (FOR LOOP, IF STATEMENT, ARRAY.PUSH) var siblings = function(node, children) { var siblingList = []; for (var n = children.length - 1; n >= 0; n--) { if (children[n] != node) { siblingList.push(children[n]); } } return siblingList; } // METHOD C (STRING.INDEXOF, ARRAY.SPLICE) var siblings = function(node, children) { siblingList = children; index = siblingList.indexOf(node); if(index != -1) { siblingList.splice(index, 1); } return siblingList; }

var className = "needle"; var element = clickedElement; while(element.previousSibling && element.previousSibling.className != className) { element = element.previousSibling; } element.previousSibling; // the element or null 结合使用.children。然后将.parentNode转换为数组后,对其进行过滤:NodeList

http://jsfiddle.net/pimvdb/DYSAm/

怎么样:

var div = document.getElementsByTagName('div')[0];
var siblings = [].slice.call(div.parentNode.children) // convert to array
                 .filter(function(v) { return v !== div }); // remove element itself
console.log(siblings);

不要告诉我这在IE8中不起作用...

HTML DOM中有一个previousSibling属性

这里有一些参考

while ( node = node.previousElementSibling ) { if ( ( ' ' + node.className + ' ' ).indexOf( 'foo' ) !== -1 ) { // found; do your thing break; } }

哪个使用while /循环函数cal dir()。 prevUntil一直持续到prevUntil: function( elem, i, until ) { return jQuery.dir( elem, "previousSibling", until ); }, previousSibling元素相同。

until

您可以使用dir: function( elem, dir, until ) { var matched = [], cur = elem[ dir ]; while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { if ( cur.nodeType === 1 ) { matched.push( cur ); } cur = cur[dir]; } return matched; }, 确定兄弟姐妹的索引是否小于目标元素的索引:

indexOf

祝你好运。>>

javascript dom siblings
7个回答
13
投票

此答案先前是针对类似问题发表的previousSiblings


7
投票

http://james.padolsey.com/jquery/使用previousSibling:


5
投票

var className = "needle"; var element = clickedElement; while(element.previousSibling && element.previousSibling.className != className) { element = element.previousSibling; } element.previousSibling; // the element or null 结合使用.children。然后将.parentNode转换为数组后,对其进行过滤:NodeList


2
投票

怎么样:


2
投票

HTML DOM中有一个previousSibling属性



1
投票

您可以使用dir: function( elem, dir, until ) { var matched = [], cur = elem[ dir ]; while ( cur && cur.nodeType !== 9 && (until === undefined || cur.nodeType !== 1 || !jQuery( cur ).is( until )) ) { if ( cur.nodeType === 1 ) { matched.push( cur ); } cur = cur[dir]; } return matched; }, 确定兄弟姐妹的索引是否小于目标元素的索引:

© www.soinside.com 2019 - 2024. All rights reserved.