简体   繁体   English

如何在jQuery中访问给定元素的上下元素

[英]How to access the upper and lower element of a given element in jquery

I have some div elements in a sequence, like : 我在序列中有一些div元素,例如:

 <div id="a"></div>
 <div id="b"></div>
 <div id="c"></div>
 <div id="d"></div>

Suppose i have id of 3rd position div which is c . 假设我的id为3rd div,即c Now i want to access the div element which placed just above and below ( in this case b & d respectively ) of div whose id is c . 现在,我要访问div元素,它位于id为c的div的上方和下方(在本例中分别为b&d)。 How can i do this in jquery? 我该如何在jquery中做到这一点?

You can use .prev() and .next() 您可以使用.prev().next()

$('#c').prev();
$('#c').next();

http://jsfiddle.net/MAd2K/ http://jsfiddle.net/MAd2K/

you can do it like this 你可以这样

$('#c').prev('div');

$('#c').next('div');

Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM