目录

#ID

描述 (Description)

元素ID选择器选择具有给定id属性的单个元素。

语法 (Syntax)

以下是使用此选择器的简单语法 -

$('#elementid')

参数 (Parameters)

以下是此选择器使用的所有参数的说明 -

  • elementid - 这将是一个元素ID。 如果id包含任何特殊字符,如句点或冒号,则必须使用反斜杠转义这些字符。

返回值 (Returns)

与任何其他jQuery选择器一样,此选择器也返回一个填充了found元素的数组。

例子 (Example)

  • $('#myid') - 选择具有给定id myid的单个元素。

  • $('div#yourid') - 选择具有给定id yourid的单个分区。

以下示例将选择第二个分区,并将黄色应用于其背景 -

<html>
   <head>
      <title>The Selecter Example</title>
      <script type = "text/javascript" 
         src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            /* This would select second division only*/
            $("#div2").css("background-color", "yellow");
         });
      </script>
   </head>
   <body>
      <div class = "big" id = "div1">
         <p>This is first division of the DOM.</p>
      </div>
      <div class = "medium" id = "div2">
         <p>This is second division of the DOM.</p>
      </div>
      <div class = "small" id = "div3">
         <p>This is third division of the DOM</p>
      </div>
   </body>
</html>

这将产生以下结果 -

新页面打开
↑回到顶部↑
WIKI教程 @2018