juqery 学习之三 选择器 可见性 元素属性 - Web前端
作者:98wpeu发布时间:2026-06-15分类:网页前端技术浏览:58
:hidden
匹配所有的不可见元素,input 元素的 type 属性为 "hidden" 的话也会被匹配到Matches all elements that are hidden, or input elements of type "hidden".
返回值
Array<Element>
示例
查找所有不可见的 tr 元素
html 代码:
<table><tr style="display:none"><td>value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
JQuery 代码:
$("tr:hidden")结果:
[ <tr style="display:none"><td>Value 1</td></tr> ]---------------------------------------------------------------------------------------
:visible
匹配所有的可见元素Matches all elements that are visible.
返回值
array<Element>
示例
查找所有可见的 tr 元素
HTML 代码:
<TABle><tr style="display:none"><td>Value 1</td></tr>
<tr><td>Value 2</td></tr>
</table>
jQuery 代码:
$("tr:visible")结果:
[ <tr><td>Value 2</td></tr> ]---------------------------------------------------------------------------------------
[attribute]
匹配包含给定属性的元素Matches elements that have the specified attribute.
返回值
Array<Element>
参数
attribute (string) : 属性名
示例
查找所有含有 id 属性的 div 元素
HTML 代码:
<div><p>Hello!</p>
</div>
<div id="test2"></div>
jquery 代码:
$("div[id]")结果:
[ <div id="test2"></div> ]---------------------------------------------------------------------------------------
[attribute=value]
匹配给定的属性是某个特定值的元素Matches elements that have the specifIEd attribute with a certAIn value.
返回值
Array<Element>
参数
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
查找所有 name 属性是 newsletter 的 input 元素
HTML 代码:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" /><input type="Checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
jquery 代码:
$("input[name='newsletter']").attr("checked", true);结果:
[ <input type="checkbox" name="newsletter" value="Hot Fuzz" checked="true" />, <input type="checkbox" name="newsletter" value="Cold Fusion" checked="true" /> ]---------------------------------------------------------------------------------------
[attribute!=value]
匹配给定的属性是不包含某个特定值的元素Matches elements that don't have the specified attribute with a certain value.
返回值
Array<Element>
参数
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
查找所有 name 属性不是 newsletter 的 input 元素
HTML 代码:
'<input type="checkbox" name="newsletter" value="Hot Fuzz" /><input type="checkbox" name="newsletter" value="Cold Fusion" />
<input type="checkbox" name="accept" value="Evil Plans" />
JQuery 代码:
$("input[name!='newsletter']").attr("checked", true);结果:
[ <input type="checkbox" name="accept" value="Evil Plans" checked="true" /> ]---------------------------------------------------------------------------------------
[attribute^=value]
匹配给定的属性是以某些值开始的元素Matches elements that have the specified attribute and it starts with a certain value.
返回值
Array<Element>
参数
attribute (String) : 属性名
value ( String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
查找所有 name 以 'news' 开始的 input 元素
HTML 代码:
<input name="newsletter" /><input name="milkman" />
<input name="newsboy" />
jQuery 代码:
$("input[name^='news']")结果:
[ <input name="newsletter" />, <input name="newsboy" /> ]---------------------------------------------------------------------------------------
[attribute$=value]
匹配给定的属性是以某些值结尾的元素Matches elements that have the specified attribute and it ends with a certain value.
返回值
Array<Element>
参数
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
查找所有 name 以 'letter' 结尾的 input 元素
HTML 代码:
<input name="newsletter" /><input name="milkman" />
<input name="jobletter" />
jQuery 代码:
$("input[name$='letter']")结果:
[ <input name="newsletter" />, <input name="jobletter" /> ]---------------------------------------------------------------------------------------
[attribute*=value]
匹配给定的属性是以包含某些值的元素Matches elements that have the specified attribute and it contains a certain value.
返回值
Array<Element>
参数
attribute (String) : 属性名
value (String) : 属性值。引号在大多数情况下是可选的。但在遇到诸如属性值包含"]"时,用以避免冲突。
示例
查找所有 name 包含 'man' 的 input 元素
HTML 代码:
<input name="man-news" /><input name="milkman" />
<input name="letterman2" />
<input name="newmilk" />
jQuery 代码:
$("input[name*='man']")结果:
[ <input name="man-news" />, <input name="milkman" />, <input name="letterman2" /> ]---------------------------------------------------------------------------------------
[selector1][selector2][selectorN]
复合属性选择器,需要同时满足多个条件时使用。Matches elements that have the specified attribute and it contains a certain value.
返回值
Array<Element>
参数
selector1 (Selector) : 属性选择器
selector2 (Selector) : 另一个属性选择器,用以进一步缩小范围
selectorN (Selector) : 任意多个属性选择器
示例
找到所有含有 id 属性,并且它的 name 属性是以 man 结尾的
HTML 代码:
<input id="man-news" name="man-news" /><input name="milkman" />
<input id="letterman" name="new-letterman" />
<input name="newmilk" />
jQuery 代码:
$("input[id][name$='man']")结果:
[ <input id="letterman" name="new-letterman" /> ]相关推荐
- Jquery倒计时源码分享 - Web前端
- jquery控制select的text/value值为选中状态 - Web前端
- jQuery 获取兄弟元素的几种不错方法 - Web前端
- jquery进行数组遍历如何跳出当前的each循环 - Web前端
- jQuery实现table隔行换色和鼠标经过变色的两种方法 - Web前端
- jquery得到iframe src属性值的方法 - Web前端
- jquery选择器使用详解 - Web前端
- jQuery实现鼠标经过图片预览大图效果 - Web前端
- HTML页面弹出居中可拖拽的自定义窗口层 - Web前端
- jquery跟js初始化加载的多种方法及区别介绍 - Web前端
- 网页前端技术排行
-
- 1【第六章】Foundation之按钮和下拉功能 - Web前端
- 2分析Iconfont-阿里巴巴矢量常用图标库 - Web前端
- 3jQuery编写widget的一些技巧分享 - Web前端
- 4在Mac/PC上远程调试iPhone/iPad上的网页 - Web前端
- 5基于jquery的滚动条滚动固定div(附演示下载) - Web前端
- 6jQuery实例教程:制作网页中可折叠的面板 - Web前端
- 7分享精心挑选的12款优秀jQuery Ajax分页插件和教程 - Web前端
- 8[Web前端]用javascript实现默认图片替代未显示的图片 - Web前端
- 9javascript原生和jquery库实现iframe自适应高度和宽度 - Web前端
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


