jquery中each的用法详解

对象访问:

length:返回对象个数;

    	$("input").click(function(){
    		console.log($("div").length);
    	});

size();返回对象个数;

    	$("input").click(function(){
    		alert($("div").size());
    	});

each:遍历

each:每个元素;

用法:元素.each(function(index,element){ })

index:索引下标

element:元素 (一个元素对应一个下标,输出时候成对输出)

<body>
    <div>div1</div>
    <div>div2</div>
    <div>div3</div>
    <input type="button" name="" value="点击">
    <script type="text/javascript">
    	$("input").click(function(){
    		$("div").each(function(index, el) {
    			console.log(index);
    		});
    	});
    </script>
</body>

输出:


注意:index是从0开始的

    	$("input").click(function(){
    		$("div").each(function(index, el) {
    			console.log(el);
    		});
    	});

输出:


猜你喜欢

转载自blog.csdn.net/weixin_41849462/article/details/80636588
今日推荐