jQuery:获取当前元素之后的下一个元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1409918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery: get immediate next element after the current element
提问by ravikiran
I need set focus to the element that is immediate next(and in other case immediate prev ) to the current element.
我需要将焦点设置为当前元素的下一个(在其他情况下为直接上一个)元素。
For example:
例如:
<li>item1</li> <- prev element (get this element)
<li>item1</li><- current element
<li>item1</li> <- next element (get this element)
<li>item1</li>
This is what I used
这是我用的
var curr = (event.target).next();
$(curr).trigger('focus');
when I check the value of curr
in firebug it shows undefined for some reason.
当我检查curr
firebug 中的值时,由于某种原因它显示未定义。
回答by geowa4
Use
用
$(event.target).next()
to get the next sibling. You can also pass an expression to the next
function. This will select the next sibling to match you selector:
得到下一个兄弟姐妹。您还可以将表达式传递给next
函数。这将选择下一个同级以匹配您的选择器:
$(event.target).next("p")
You can also use nextAll
, which works the same way but returns all of the following sublings. See more here. There is also, prev
and prevAll
, which are the analogues for getting the previous element(s).
您也可以使用nextAll
,它的工作方式相同,但返回以下所有 sublings。在此处查看更多信息。还有, prev
and prevAll
,它们是获取前一个元素的类似物。
Basically, you were really close, you just need to wrap event.target
in the jQuery object, as event.target
returns the actual element.
基本上,你真的很接近,你只需要包装event.target
在 jQuery 对象中,作为event.target
返回实际元素。
回答by Dan F
Are you inside a jquery event handler? If so, why not use $(this).next()
and/or $(this).prev()
?
您在 jquery 事件处理程序中吗?如果是这样,为什么不使用$(this).next()
和/或$(this).prev()
?
回答by nothrow
var curr = (event.target).nextSibling;