小码哥的IT人生

首页 > JS > jQuery

jQuery :first 选择器 概述

jQuery 2022-06-01 11:59:31小码哥的IT人生shichen

jQuery :first 选择器

实例

选择第一个 <p> 元素:

$("p:first")

完整实例:

<html>
<head>
<script type="text/javascript" src="https://apps.bdimg.com/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript"> 
$(document).ready(function(){
    $("p:first").css("background-color","#B2E0FF");
});
</script>	
</head>
<body>
<html>
<body>
<h1>Welcome to My Homepage</h1>
<p class="intro">My name is Donald</p>
<p>I live in Duckburg</p>
<p>My best friend is Mickey</p>
<div id="choose">
Who is your favourite:
<ul>
<li>Goofy</li>
<li>Mickey</li>
<li>Pluto</li>
</ul>
</div>
</body>
</html>
</body>
</html>

可以使用本站在线JavaScript测试工具测试上述代码运行效果:http://www.phpcodeweb.com/runjs.html

定义和用法

:first 选择器选取第一个元素。

最常见的用法:与其他元素一起使用,选取指定组合中的第一个元素(就像上面的例子)。

语法

$(":first")

版权所有 © 小码哥的IT人生
Copyright © phpcodeweb All Rights Reserved
ICP备案号:苏ICP备17019232号-2  

苏公网安备 32030202000762号

© 2021-2024