jQuery :first 選擇器


jQuery 中的 :first 選擇器用於選擇第一個 DOM 元素。

語法

語法如下 −

$(":first")

示例

現在,讓我們看一個示例來實現 :first() 選擇器 −

<!DOCTYPE html>
<html>
<head>
<style>
   .demo {
      background-color: red;
      color: white;
      font-size: 16px;
      border: 2px blue dashed;
   }
</style>
<script src="https://code.jquery.com/jquery-3.4.1.js"></script>
<script>
   $(document).ready(function(){
      $("input:first").addClass("demo");
   });
</script>
</head>
<body>
<h2>Job Application</h2>
<form action="">
Applicant Name: <input type="text" name="cname"><br>
Rank: <input type="number" name="rank"><br><br>
Resume: <input type="file" name="myfile">
</form>
</body>
</html>

輸出

這將產生以下輸出 −

更新時間:05-11-2019

204 次瀏覽

開始你的職業

透過完成課程獲得認證

開始
廣告
© . All rights reserved.