jQuery 基础 选择器

本文介绍了在前端开发中如何使用不同的技术来选择网页元素,包括原生JavaScript、CSS及jQuery的选择器,涵盖了基本选择器、属性选择器等,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,JavaScript 选择元素

- document.getElementById() 【返回的是元素】

- document.getElementsByName()  【返回的是NodeList】

- document.getElementsByTagName()  【返回的是HTMLCollection】

- document.getElementsByClassName()   【返回的是HTMLCollection】

- document.querySelector()   【使用css 选择器,如:".div1"】(第一个匹配)

- document.querySelectorAll()   【同上】 (返回的是NodeList)


CSS 选择元素

- 基本选择器

- 属性选择器

- 伪类选择器

- 伪元素选择器


jQuery 选择元素

jQuery 选择器会返回一个 jQuery 对象的集合,是一个类数组对象,其顺序与页面中顺序一致。

注意:

ready方法:

$(document).ready(function(){});
的简写:

$(function(){});


类数组的例子:

var a = {0:1,length = 1;};
类数组转换为数组:

a = Array.prototype.slice.call(a);


选择器的例子:

$(function(){
			var element1 = $('#box1,#box2');
			var element2 = $('#box2');
			var element3 = $('.box');
			var element4 = $('[name="username"]');
			var element5 = $('div');
			var element6 = $(document.getElementById('box1'));

			console.log(element1);
			console.log(element2);
			console.log(element3);
			console.log(element4);
			console.log(element5);
			console.log(element6);
		});




- jQuery 基本选择器

(a) ID 选择器

(b) 类选择器

(c) 元素选择器

(d) 后代选择器

(e) 属性选择器


d. 后代选择器

<script type="text/javascript">
		$('.list > li').addClass('highlight');
	</script>

e. 属性选择器

	<script type="text/javascript">
		// $('.list > li').addClass('highlight');
		$('a[href="sdg"]').addClass('highlight');
	</script>




- jQuery 筛选器/过滤器

(a)位置筛选器

(b)子元素筛选器

(c)表单筛选器

(d)内容筛选器

(e)其他筛选器

(f)自定义筛选器


 a. 位置筛选器

     :first; :last; :even; :odd; :eq(n); gt(n); lt(n).后面三个分别为:等于,大于,小于。且n从零开始。

	<script type="text/javascript">
		$('li:first').addClass('highlight');
		$('li:last').addClass('highlight');
		$('li:even').addClass('highlight');
		$('li:odd').addClass('highlight');
		$('li:eq(2)').addClass('highlight');
		$('li:gt(2)').addClass('highlight');
		$('li:lt(2)').addClass('highlight');
	</script>


b. 子元素筛选器

     :first-child; :last-child; :first-of-type; :last-of-type; nth-child(); :nth-last-child(); :nth-of-type;  nth-last-type; only-child; only-of-type.

	<script type="text/javascript">
		// 子元素筛选器
		$('li:first-child').addClass('highlight');
		$('li:last-child').addClass('highlight');
		$('li:nth-child(2n)').addClass('highlight');
	</script>


c. 表单筛选器 

     :checked; :disabled; :enabled; :focus; :button; :checkbox; :file; :image; :input; :password; :radio; :reset; :selected; :submit; :text.

	<script type="text/javascript">
		$(':checked').parent().addClass('pa');
	</script>


d. 内容筛选器

     :empty; :contains(text); :has(selector); parent; 

	<script type="text/javascript">
		// 内容筛选器
		$(':empty').addClass('pa');
		$(':contains("abc")').addClass('pa');
		$('p:has(span)').addClass('pa');
		$('p:parent').addClass('pa');
	</script>


e. 其他筛选器

     :lang(language); :not(selector); :root; :target; :hidden; :visible; :header; :animated.


jQuery 选择器的性能优化

- 尽量使用CSS中有的选择器

- 避免过度约束

- 尽量以ID开头

- 让选择器的右边有更多特征

- 避免使用全局选择器

- 缓存选择器结果

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值