jQuery使用总结 - Core jQuery Selectors 选择器一2/4
CSS基础
需要对CSS有初步的了解,如下是一些常见的举例,更深入的可以参考相关的资料
body,th,td body.fancy body.fancy h1 |
设置多个元素的风格 |
#pageContent |
元素名pageContent的风格 <div id=” pageContent” … |
img[src="example.jpg"] |
使用css选择器设置特定的img元素风格 |
.rightCap |
定义一个类别的风格 <div class=” rightCap” … |
jQuery’s $() function
$作为一个命名空间的作用,可以算是jQuery的别名
var trimmed = $.trim(someString) 等同于var trimmed = jQuery.trim(someString);
基本选择器
CSS选择器 属性选择器举例和说明
$(“a”) |
Matches all anchor (<a>) elements |
#specialID |
Matches the element with the id value of specialID |
.specialClass |
Matches all elements with the class specialClass |
a#specialID.specialClass |
Matches the element with the id value specialID if it’s an anchor tag and has class specialClass |
p a.specialClass |
Matches all anchor elements with the class specialClass that are descendants of <p> elements |
$('div,span') |
select all <div> and all <span> elements |
* |
Matches any element. |
E |
Matches all elements with tag name E. |
E F |
Matches all elements with tag name F that are descendants of E. |
E>F |
Matches all elements with tag name F that are direct children of E. |
E+F |
Matches all elements with tag name F that are immediately preceded by sibling E. |
E~F |
Matches all elements with tag name F preceded by any sibling E. |
E.C |
Matches all elements with tag name E with class name C. Omitting E is the same as *.C. |
E#I |
Matches all elements with tag name E with the id of I. Omitting E is the same as *#I. |
E[A] |
Matches all elements with tag name E that have attribute A of any value. |
E[A=V] |
Matches all elements with tag name E that have attribute A whose value is exactly V. |
E[A^=V] |
Matches all elements with tag name E that have attribute A whose value starts with V. |
E[A$=V] |
Matches all elements with tag name E that have attribute A whose value ends with V. |
E[A!=V] |
Matches all elements with tag name E that have attribute A whose value doesn’t match the value V, or that lack attribute A completely. |
E[A*=V] |
Matches all elements with tag name E that have attribute A whose value contains V. |
过滤器
There are a whole slew of these selectors, some defined by CSS, others specific to jQuery, and they can provide surprisingly elegant solutions to sometimes tough problems. The CSS specification refers to these types of selectors as pseudo-classes, but jQuery has adopted the crisper term filters, because each of these selectors filter a base selector. These filter selectors are easy to spot, as they all begin with the colon (:) character. And remember, if you omit any base selector, it defaults to *.
table#languages td:first-child
table#languages td:nth-child(1)
if we want to select only enabled and checked checkboxes, we could use
$(“:checkbox:checked:enabled”)
find which table row contains a particular image element that can be uniquely identified using its src attribute
="puppy.png"]
选择的元素集合操作
var imgElement = $('img[alt]')[0]
$('p').add('<div>Hi there!</div>')
$('img').addClass('seeThrough').filter('[title*=dog]').addClass('thickBorder')
$('div').has('img[alt]')
$('img').each(function(n){
this.alt='This is image['+n+'] with an id of '+this.id;
});
$(this).closest('div')
函数有:
size get eq first last toArray index
add not filter slice has map each find is
jQuery chains
$('img').filter('[title]').hide();
The filter() method returns the set of titled images, but by calling end() we back up to the previous wrapped set (the original set of all images), which gets operated on by the addClass() method. Without the intervening end() method, addClass() would have operated on the set of clones.
$('img').filter('[title]').hide().end().addClass('anImage');
This statement selects all <div> elements, adds class a to them, creates a new wrapped set consisting of all <img> elements that are descendants of those <div> elements, applies class b to them, creates a third wrapped set that’s a merger of the <div> elements and their descendant <img> elements, and applies class c to them.
Whew! At the end of it all, the <div> elements end up with classes a and c, whereas
the images that are descendants of those elements are given classes b and c.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南