jQuery - 选择器(三)
时间:2020-09-14 09:35:39
收藏:0
阅读:69
jQuery 选择器
- jQuery 选择器允许您对 HTML 元素组或单个元素进行操作
- jQuery 选择器基于元素的 id、类、类型、属性、属性值等"查找"(或选择)HTML 元素。 它基于已经存在的 CSS 选择器,除此之外,它还有一些自定义的选择器
- jQuery 中所有选择器都以美元符号开头:$()
jQuery 元素选择器
- jQuery 元素选择器基于元素名选取元素
// 用户点击按钮后,隐藏所有 <p> 元素
$(document).ready(function () {
$("button").click(function () {
$("p").hide();
}) ;
});
jQuery #id 选择器
- jQuery #id 选择器通过 HTML 元素的 id 属性选取指定的元素
// 用户点击按钮后,所有带属性 id="test" 的元素将被隐藏
$(doccument).ready(function () {
$("button").click(function () {
$("#test").hide();
}) ;
});
jQuery .class 选择器
- jQuery 类选择器可以通过指定的 class 查找元素
// 用户点击按钮之后,所有带属性 class="test" 的元素将被隐藏
$(document).ready(function () {
$("button").click(function () {
$(".test").hide();
}) ;
});
jQuery 属性选择器
- jQuery 属性选择器可以查找带有某个属性的元素
// 用户点击按钮之后,所有带属性 href 的元素将被隐藏
$(document).ready(function () {
$("button").click(function () {
$("[href]").hide();
}) ;
});
jQuery 更多选择器
语法 | 描述 |
---|---|
$("*") | 选取所有元素 |
$(this) | 选取当前元素 |
$("p.intro") | 选取 class 为 intro 的 <p> 元素 |
$("p:first") | 选取第一个 <p> 元素 |
$("ul li:first") | 选取第一个 <ul> 元素的第一个 <li> 元素 |
$("ul li:first-child") | 选取每个 <ul> 元素的第一个 <li> 元素 |
$("[href]") | 选取所有带有 href 属性的元素 |
$("a[target=‘_blank‘]") | 选取所有 target 属性值等于 "_blank" 的 <a> 元素 |
$("a[target!=‘_blank‘]") | 选取所有 target 属性值不等于 "_blank" 的 <a> 元素 |
$(":button") | 选取所有 type="button" 的 <input> 元素和 <button> 元素 |
$("tr:even") | 选取偶数位置的 <tr> 元素 |
$("tr:odd") | 选取奇数位置的 <tr> 元素 |
原文:https://www.cnblogs.com/xdy-/p/13645200.html
评论(0)