JQuery选择器
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JQuery选择器,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含13598字,纯文字阅读大概需要20分钟。
内容图文
![JQuery选择器](/upload/InfoBanner/zyjiaocheng/1060/00b68e696d0246a6a97e1a77849681ba.jpg)
选择器
1.基本选择器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>基本选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 180px;
height: 180px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div .mini01{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
</style>
<script type="text/javascript">
// <input type="button" value="改变 id 为 one 的元素的背景色为 红色" />
$(function () {
$("#b1").click(function () {
$("#one").css("backgroundColor","red");
})
// <input type="button" value=" 改变元素名为 <div> 的所有元素的背景色为 红色" />
$("#b2").click(function () {
$("div").css("backgroundColor","red");
})
// <input type="button" value=" 改变 class 为 mini 的所有元素的背景色为 红色" />
$("#b3").click(function () {
$(".mini").css("backgroundColor","red");
})
//<input type="button" value=" 改变所有的<span>元素和 id 为 two 的元素的背景色为红色" />
$("#b4").click(function () {
$("span,#two").css("backgroundColor","red");
})
})
</script>
</head>
<body>
<input type="button" value="保存" name="ok" />
<input type="button" value="改变 id 为 one 的元素的背景色为 红色" />
<input type="button" value=" 改变元素名为 <div> 的所有元素的背景色为 红色" />
<input type="button" value=" 改变 class 为 mini 的所有元素的背景色为 红色" />
<input type="button" value=" 改变所有的<span>元素和 id 为 two 的元素的背景色为红色" />
<h1>有一种奇迹叫坚持</h1>
<h2>自信源于努力</h2>
<div >
id为one
</div>
<div >
id为two class是 mini
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini</div>
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini01</div>
<div >class是 mini</div>
</div>
<span >class为spanone的span元素</span>
<span >class为mini的span元素</span>
<input type="text" value="zhang" name="username">
</body>
</html>
2.属性选择器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>属性过滤选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 180px;
height: 180px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div .mini01{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div.visible{
display:none;
}
</style>
<script type="text/javascript">
$(function () {
// <input type="button" value=" 含有属性title 的div元素背景色为红色" />
$("#b1").click(function () {
$("div[title]").css("backgroundColor","red");
})
//<input type="button" value=" 属性title值等于test的div元素背景色为红色" />
$("#b2").click(function () {
$("div[title=‘test‘]").css("backgroundColor","red");
})
// <input type="button" value=" 属性title值不等于test的div元素(没有属性title的也将被选中)背景色为红色" />
$("#b3").click(function () {
$("div[title!=‘test‘]").css("backgroundColor","red");
})
//<input type="button" value=" 属性title值 以te开始 的div元素背景色为红色" />
$("#b4").click(function () {
$("div[title^=‘te‘]").css("backgroundColor","red");
})
//<input type="button" value=" 属性title值 以est结束 的div元素背景色为红色" />
$("#b5").click(function () {
$("div[title$=‘est‘]").css("backgroundColor","red");
})
// <input type="button" value="属性title值 含有es的div元素背景色为红色" />
$("#b6").click(function () {
$("div[title*=‘es‘]").css("backgroundColor","red");
})
// <input type="button" value="选取有属性id的div元素,然后在结果中选取属性title值含有“es”的 div 元素背景色为红色" />
$("#b7").click(function () {
$("div[id][title*=‘es‘]").css("backgroundColor","red");
})
})
</script>
</head>
<body>
<input type="button" value="保存" name="ok" />
<input type="button" value=" 含有属性title 的div元素背景色为红色" />
<input type="button" value=" 属性title值等于test的div元素背景色为红色" />
<input type="button" value=" 属性title值不等于test的div元素(没有属性title的也将被选中)背景色为红色" />
<input type="button" value=" 属性title值 以te开始 的div元素背景色为红色" />
<input type="button" value=" 属性title值 以est结束 的div元素背景色为红色" />
<input type="button" value="属性title值 含有es的div元素背景色为红色" />
<input type="button" value="选取有属性id的div元素,然后在结果中选取属性title值含有“es”的 div 元素背景色为红色" />
<div >
id为one div
</div>
<div title="test">
id为two class是 mini div title="test"
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini</div>
<div >class是 mini</div>
</div>
<div title="test02">
class是 one title="test02"
<div >class是 mini01</div>
<div >class是 mini</div>
</div>
<div >
这是隐藏的
</div>
<div >
</div>
<div >
动画
</div>
<input type="text" value="zhang" name="username">
</body>
</html>
3.层次选择器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>层次选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 180px;
height: 180px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div .mini01{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
</style>
<script type="text/javascript">
$(function () {
// <input type="button" value=" 改变 <body> 内所有 <div> 的背景色为红色" />
$("#b1").click(function () {
$("body div").css("backgroundColor","red");
})
// <input type="button" value=" 改变 <body> 内子 <div> 的背景色为 红色" />
$("#b2").click(function () {
$("body>div").css("backgroundColor","red");
})
})
</script>
</head>
<body>
<input type="button" value="保存" name="ok" />
<input type="button" value=" 改变 <body> 内所有 <div> 的背景色为红色" />
<input type="button" value=" 改变 <body> 内子 <div> 的背景色为 红色" />
<h1>有一种奇迹叫坚持</h1>
<h2>自信源于努力</h2>
<div >
id为one
</div>
<div >
id为two class是 mini
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini</div>
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini01</div>
<div >class是 mini</div>
</div>
<span > span
</span>
</body>
</html>
4.过滤选择器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>基本过滤选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 180px;
height: 180px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div .mini01{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
</style>
<script type="text/javascript">
$(function () {
// <input type="button" value=" 改变第一个 div 元素的背景色为 红色" />
$("#b1").click(function () {
$("div:first").css("backgroundColor","red");
})
// <input type="button" value=" 改变最后一个 div 元素的背景色为 红色" />
$("#b2").click(function () {
$("div:last").css("backgroundColor","red");
})
// <input type="button" value=" 改变class不为 one 的所有 div 元素的背景色为 红色" />
$("#b3").click(function () {
$("div:not(.one)").css("backgroundColor","red");
})
// <input type="button" value=" 改变索引值为偶数的 div 元素的背景色为 红色" />
$("#b4").click(function () {
$("div:even").css("backgroundColor","red");
})
// <input type="button" value=" 改变索引值为奇数的 div 元素的背景色为 红色" />
$("#b5").click(function () {
$("div:odd").css("backgroundColor","red");
})
// <input type="button" value=" 改变索引值为大于 3 的 div 元素的背景色为 红色" />
$("#b6").click(function () {
$("div:gt(3)").css("backgroundColor","red");
})
// <input type="button" value=" 改变索引值为等于 3 的 div 元素的背景色为 红色" />
$("#b7").click(function () {
$("div:eq(3)").css("backgroundColor","red");
})
// <input type="button" value=" 改变索引值为小于 3 的 div 元素的背景色为 红色" />
$("#b8").click(function () {
$("div:lt(3)").css("backgroundColor","red");
})
// <input type="button" value=" 改变所有的标题元素的背景色为 红色" />
$("#b9").click(function () {
$(":header").css("backgroundColor","red");
})
})
</script>
</head>
<body>
<input type="button" value="保存" name="ok" />
<input type="button" value=" 改变第一个 div 元素的背景色为 红色" />
<input type="button" value=" 改变最后一个 div 元素的背景色为 红色" />
<input type="button" value=" 改变class不为 one 的所有 div 元素的背景色为 红色" />
<input type="button" value=" 改变索引值为偶数的 div 元素的背景色为 红色" />
<input type="button" value=" 改变索引值为奇数的 div 元素的背景色为 红色" />
<input type="button" value=" 改变索引值为大于 3 的 div 元素的背景色为 红色" />
<input type="button" value=" 改变索引值为等于 3 的 div 元素的背景色为 红色" />
<input type="button" value=" 改变索引值为小于 3 的 div 元素的背景色为 红色" />
<input type="button" value=" 改变所有的标题元素的背景色为 红色" />
<h1>有一种奇迹叫坚持</h1>
<h2>自信源于努力</h2>
<div >
id为one
</div>
<div >
id为two class是 mini
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini</div>
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini01</div>
<div >class是 mini</div>
</div>
</body>
</html>
5.表单过滤选择器
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>表单属性过滤选择器</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script src="../js/jquery-3.3.1.min.js"></script>
<style type="text/css">
div,span{
width: 180px;
height: 180px;
margin: 20px;
background: #9999CC;
border: #000 1px solid;
float:left;
font-size: 17px;
font-family:Roman;
}
div .mini{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
div .mini01{
width: 50px;
height: 50px;
background: #CC66FF;
border: #000 1px solid;
font-size: 12px;
font-family:Roman;
}
#job{
margin: 20px;
}
#edu{
margin-top:-70px;
}
</style>
<script type="text/javascript">
$(function () {
// <input type="button" value=" 利用 jQuery 对象的 val() 方法改变表单内可用 <input> 元素的值" />
$("#b1").click(function () {
$("input[type=‘text‘]:enabled").val("aaa");
})
// <input type="button" value=" 利用 jQuery 对象的 val() 方法改变表单内不可用 <input> 元素的值" />
$("#b2").click(function () {
$("input[type=‘text‘]:disabled").val("bbb");
})
// <input type="button" value=" 利用 jQuery 对象的 length 属性获取复选框选中的个数" />
$("#b3").click(function () {
alert($("input[type=‘checkbox‘]:checked").length);
})
// <input type="button" value=" 利用 jQuery 对象的 length 属性获取下拉框选中的个数" />
$("#b4").click(function () {
alert($("#job>option:selected").length);
})
})
</script>
</head>
<body>
<input type="button" value="保存" name="ok" />
<input type="button" value=" 利用 jQuery 对象的 val() 方法改变表单内可用 <input> 元素的值" />
<input type="button" value=" 利用 jQuery 对象的 val() 方法改变表单内不可用 <input> 元素的值" />
<input type="button" value=" 利用 jQuery 对象的 length 属性获取复选框选中的个数" />
<input type="button" value=" 利用 jQuery 对象的 length 属性获取下拉框选中的个数" />
<br><br>
<input type="text" value="不可用值1" disabled="disabled">
<input type="text" value="可用值1" >
<input type="text" value="不可用值2" disabled="disabled">
<input type="text" value="可用值2" >
<br><br>
<input type="checkbox" name="items" value="美容" >美容
<input type="checkbox" name="items" value="IT" >IT
<input type="checkbox" name="items" value="金融" >金融
<input type="checkbox" name="items" value="管理" >管理
<br><br>
<input type="radio" name="sex" value="男" >男
<input type="radio" name="sex" value="女" >女
<br><br>
<select name="job" multiple="multiple" size=4>
<option>程序员</option>
<option>中级程序员</option>
<option>高级程序员</option>
<option>系统分析师</option>
</select>
<select name="edu" >
<option>本科</option>
<option>博士</option>
<option>硕士</option>
<option>大专</option>
</select>
<br/>
<div >
id为two class是 mini div
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini</div>
<div >class是 mini</div>
</div>
<div >
class是 one
<div >class是 mini01</div>
<div >class是 mini</div>
</div>
</body>
</html>
原文:https://www.cnblogs.com/dss-99/p/14992430.html
内容总结
以上是互联网集市为您收集整理的JQuery选择器全部内容,希望文章能够帮你解决JQuery选择器所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。