jQuery #id选择器用法指南

2021年3月9日16:23:15 发表评论 630 次浏览

#id选择器指定要选择的元素的ID。它不能以数字开头, 并且id属性在文档中必须唯一, 这意味着它只能使用一次。

语法如下:

$("#id")

参数:

  • ID:元素的特定ID。

示例1:

<!DOCTYPE html>
< html >
  
< head >
     < script src =
"https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" >
   </ script >
     
   < script >
     $(document).ready(function() {
     $("#Geeks").css("background-color", "red");
       });
   </ script >
</ head >
  
< body >
  
     < h1 >GEEKS FOR GEEKS</ h1 >
  
     < p id = "Geeks" >jQuery|#id selector
   </ p >
  
</ body >
  
</ html >

输出如下:

jQuery | #id选择器1

示例2:

<!DOCTYPE html>
< html lang = "en" >
  
< head >
     < meta charset = "utf-8" >
     < title >ID</ title >
     < style >
         div {
             width: 100px;
             height: 100px;
             float: left;
             padding: 10px;
             margin: 10px;
             background-color: pink;
         }
     </ style >
     < script src =
"https://code.jquery.com/jquery-1.10.2.js" >
   </ script >
</ head >
  
< body >
  
     < div id = "DIV1" >
         < p >id="DIV1"</ p >
     </ div >
     < div id = "DIV2" >id="DIV2"</ div >
  
     < script >
         $("#DIV2").css("border", "2px solid yellow");
     </ script >
  
</ body >
  
</ html >

输出如下:

jQuery | #id选择器2

支持的浏览器:

  • 谷歌浏览器
  • 火狐浏览器
  • 边缘
  • 苹果浏览器
  • 歌剧

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: