Gnu*_*fo1 16 jquery jquery-selectors next
next,prev,nextAll和prevAll方法非常有用,但如果您尝试查找的元素不在同一个父元素中,则不会.我想做的是这样的:
<div>
<span id="click">Hello</span>
</div>
<div>
<p class="find">World></p>
</div>
Run Code Online (Sandbox Code Playgroud)
当click按下具有id的span时,我想将下一个元素与类匹配find,在这种情况下,该类不是被点击元素的兄弟,所以next()或nextAll()不起作用.
lor*_*lad 13
试试这个.它将标记您的元素,创建一组与您的选择器匹配的元素,并从您的元素后面的集合中收集所有元素.
$.fn.findNext = function ( selector ) {
var set = $( [] ), found = false;
$( this ).attr( "findNext" , "true" );
$( selector ).each( function( i , element ) {
element = $( element );
if ( found == true ) set = set.add( element )
if ( element.attr("findNext") == "true" ) found = true;
})
$( this ).removeAttr( "findNext" )
return set
}
Run Code Online (Sandbox Code Playgroud)
编辑
使用jquerys索引方法更简单的解决方案.您调用方法的元素需要由同一个选择器选择
$.fn.findNext = function( selector ){
var set = $( selector );
return set.eq( set.index( this, ) + 1 )
}
Run Code Online (Sandbox Code Playgroud)
为了解决这个障碍的功能,我们可以使用浏览器自己的compareDocumentposition
$.fn.findNext = function ( selector ) {
// if the stack is empty, return the first found element
if ( this.length < 1 ) return $(s).first();
var found,
that = this.get(0);
$( selector )
.each( function () {
var pos = that.compareDocumentPosition( this );
if ( pos === 4 || pos === 12 || pos === 20 ){
// pos === 2 || 10 || 18 for previous elements
found = element;
return false;
}
})
// using pushStack, one can now go back to the previous elements like this
// $("#someid").findNext("div").remove().end().attr("id")
// will now return "someid"
return this.pushStack( [ found ] );
},
Run Code Online (Sandbox Code Playgroud)
编辑2 使用jQuery的$ .grep要容易得多.这是新代码
$.fn.findNextAll = function( selector ){
var that = this[ 0 ],
selection = $( selector ).get();
return this.pushStack(
// if there are no elements in the original selection return everything
!that && selection ||
$.grep( selection, function( n ){
return [4,12,20].indexOf( that.compareDocumentPosition( n ) ) > -1
// if you are looking for previous elements it should be [2,10,18]
})
);
}
$.fn.findNext = function( selector ){
return this.pushStack( this.findNextAll( selector ).first() );
}
Run Code Online (Sandbox Code Playgroud)
当压缩变量名称时,这变成仅仅两个班轮.
使用按位运算编辑3,此功能可能更快?
$.fn.findNextAll = function( selector ){
var that = this[ 0 ],
selection = $( selector ).get();
return this.pushStack(
!that && selection || $.grep( selection, function(n){
return that.compareDocumentPosition(n) & (1<<2);
// if you are looking for previous elements it should be & (1<<1);
})
);
}
$.fn.findNext = function( selector ){
return this.pushStack( this.findNextAll( selector ).first() );
}
Run Code Online (Sandbox Code Playgroud)
我今天自己正在研究这个问题,这就是我想出的:
/**
* Find the next element matching a certain selector. Differs from next() in
* that it searches outside the current element's parent.
*
* @param selector The selector to search for
* @param steps (optional) The number of steps to search, the default is 1
* @param scope (optional) The scope to search in, the default is document wide
*/
$.fn.findNext = function(selector, steps, scope)
{
// Steps given? Then parse to int
if (steps)
{
steps = Math.floor(steps);
}
else if (steps === 0)
{
// Stupid case :)
return this;
}
else
{
// Else, try the easy way
var next = this.next(selector);
if (next.length)
return next;
// Easy way failed, try the hard way :)
steps = 1;
}
// Set scope to document or user-defined
scope = (scope) ? $(scope) : $(document);
// Find kids that match selector: used as exclusion filter
var kids = this.find(selector);
// Find in parent(s)
hay = $(this);
while(hay[0] != scope[0])
{
// Move up one level
hay = hay.parent();
// Select all kids of parent
// - excluding kids of current element (next != inside),
// - add current element (will be added in document order)
var rs = hay.find(selector).not(kids).add($(this));
// Move the desired number of steps
var id = rs.index(this) + steps;
// Result found? then return
if (id > -1 && id < rs.length)
return $(rs[id]);
}
// Return empty result
return $([]);
}
Run Code Online (Sandbox Code Playgroud)
所以在你的例子中
<div><span id="click">hello</span></div>
<div><p class="find">world></p></div>
Run Code Online (Sandbox Code Playgroud)
你现在可以使用找到并操纵'p'元素
$('#click').findNext('.find').html('testing 123');
Run Code Online (Sandbox Code Playgroud)
我怀疑它在大型结构上表现不错,但在这里它是:)