jQuery 中的 #ID函数

首页 / jQuery入门教程 / jQuery 中的 #ID函数

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

#ID - 语法

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

$('#elementid')
  • elementid -这将是一个元素ID。

#ID - 返回值

像任何其他jQueryselector一样,此选择器还返回一个填充有find元素的数组。

#ID - 示例

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

  • $('div#yourid')  -  选择具有给定<div id='yourid'>的单个元素。

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

链接:https://www.learnfk.comhttps://www.learnfk.com/jquery/selector-element-id.html

来源:LearnFk无涯教程网

<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>

这将产生以下输出-

无涯教程网

This is first division of the DOM.

This is second division of the DOM.

This is third division of the DOM

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

从0开始学大数据 -〔李智慧〕

Python核心技术与实战 -〔景霄〕

Elasticsearch核心技术与实战 -〔阮一鸣〕

设计模式之美 -〔王争〕

打造爆款短视频 -〔周维〕

操作系统实战45讲 -〔彭东〕

数据分析思维课 -〔郭炜〕

超级访谈:对话毕玄 -〔毕玄〕

互联网人的数字化企业生存指南 -〔沈欣〕

好记忆不如烂笔头。留下您的足迹吧 :)