jquery和JavaScript的转换,size(),each(),index(),data()

js对象和jQuery对象的方法不能共用。


js对象--->jquery对象

$(js的dom对象)


jquery对象--->js对象

$('jquery选择器')[index].js的dom方法,

$('jquery选择器').get(index).js的dom方法

注:outHtml是js返回本身标签的方法


size()和length属性的用处一样滴


each()和index()看下面的demo

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
	</head>
	<body>
		<div id="box">
			<a>hhh111</a>
			<a>hhh222</a>
			<a>hhh333</a>
			<a>hhh444</a>
			<a>hhh555</a>
			<a>hhh666</a>
			<a>hhh777</a>
		</div>
		<script>
			$(function(){
				$("a").each(function(i,e){//e可用jQuery的this对象替代
					$(e).attr({"href":"#"+i});
					console.log($(e).index("#box a"));
				})
//				$("a").each(function(i){
//					$(this).attr({"href":"#"+i});
//					console.log($(this).index("#box a"));
//				})
			})
		</script>
	</body>
</html>

data():给jQuery对象添加值,并未改变标签

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
	</head>
	<body>
		<div id="box">
			<a>hhh111</a>
			<a>hhh222</a>
			<a>hhh333</a>
			<a>hhh444</a>
			<a>hhh555</a>
			<a>hhh666</a>
			<a>hhh777</a>
		</div>
		<script>
			$(function(){
				$("a").each(function(i,e){//e可用jQuery的this对象替代
					$(e).data({"href":"#"+i});
					console.log($(e).data("href"));
				})
			})
		</script>
	</body>
</html>

猜你喜欢

转载自blog.csdn.net/qq_40002902/article/details/80197440
今日推荐