视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
jqueryprev函数实例
2020-11-27 20:17:33 责编:小采
文档
 prev() 函数被用来匹配元素集的前一个兄弟元素,仅仅只有前一个兄弟元素被选择,其子元素将被忽略。

prev() 函数可以允许通过选择器来过滤,例如 prev(‘p’) 被用来选择匹配元素的前一个元素是兄弟元素的p元素。

具体实例代码:

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

效果:

下载本文
显示全文
专题