jquery next函数实例

next() 函数获得紧跟着匹配元素的下一个兄弟元素标签,仅选择的是兄弟,而非孩子元素。

 

next() 函数可以允许通过选择器来进行过滤,如next(‘div’)被用来选择紧跟着的div兄弟元素 如紧邻的兄弟元素不为div标签,则不会选择任何元素。

具体实例代码:

<html>
	<head>
		<style type="text/css">
			 div,p{
			 	width:110px;
			 	height:40px;
			 	margin:2px 8px 64px 8px;
			 	float : left;
			 	border :1px blue solid;
			 	
			 	
			 	}
			
			</style>
			<script type="text/javascript" src="../jquery-1.11.1.min.js"></script>
			
			
	</head>
	
	<body>
         <h1>jquery next() example</h1>
         <div id="start">this is div1
         	<div>div 1 child</div>
         	
         	</div>
         	<p>this is paragrap 1</p>
         	<div>
         		this is div2
         		<div>div2 child</div>
         		</div>
         		<div>this is div3
         			<div>div 3 child</div>
         			</div>
         			<br/><br/><br/>
               
              <br/><br/><br/>
              <button id="nextButton1">next()</button>
              <button id="nextButton2">next('div')</button>
              <button id="nextButton3">next('p')</button>
              <button id="reset">Rest</button>
              <script type="text/javascript">
              	var $currElement=$("#start");
              	$currElement.css("background","red");
              	$('#nextButton1').click(function(){
              		if(!$currElement.next().length){
              			alert("no element found!");
              			return false;
              			
              			}
              			$currElement=$currElement.next();
              			$('div,p').css('background',"");
              			$currElement.css("background","red");            		
              		});
              	$("#nextButton2").click(function(){
              		if(!$currElement.next('div').length){
              			alert("no element found");
              			return false;
              			
              			
              			}
              		$currElement=$currElement.next('div');
              		$("div,p").css("background","");
              		$currElement.css("background","red");
              		
              		});
              	$("#nextButton3").click(function(){
              		if(!$currElement.next('p').length){
              			alert("no element found");
              			return false;
              			
              			}
              			$currElement=$currElement.next('p');
              			$('div,p').css("background","");
              			$currElement.css("background","red");
              		
              		
              		});
              	$("#reset").click(function(){
              		location.reload();
              		
              		});
              	
              	</script>
	</body>
</html>

效果图:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值