jQuery Selectors(选择器)的使用(一、基本篇)

  作者:bea

我的学习方法:先入门,后进阶!本系列文章分为:基本篇、层次篇、简单篇、内容篇、可见性篇、属性篇、子元素篇、表单篇、表单对象属性篇共9篇文章。详细介绍页面 http://demo./html/jquery/jQuery-Selector.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitiona
我的学习方法:先入门,后进阶!本系列文章分为:基本篇、层次篇、简单篇、内容篇、可见性篇、属性篇、子元素篇、表单篇、表单对象属性篇共9篇文章。

详细介绍页面
http://demo./html/jquery/jQuery-Selector.html



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>jQuery-Selectors</title>
<script src="http://img./jslib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<style type="text/css">
.div
{
width:95%;
margin-left:15px;
margin-top:15px;
margin-right:15px;
padding-left:5px;
padding-top:5px;
padding-bottom:5px;
background-color:#ccc;
border:#999 1px solid;
line-height:30px;
font-size:13px;
font-family:微软雅黑;
}
.blue{color:Blue;}
.green{color:Green;}
.button{border:green 1px solid;width:100px;}
.xiaobiao{ font-weight:bold;}
.red_test{background-color:red; color:White; width:300px; height:30px;}
.li_test{border:#000 1px solid;}
</style>
</head>

<body>

<div class="div">
<div style="width:100%; text-align:center; font-size:16px; font-weight:bold;">jQuery-Selectors(选择器)的使用(一、基本篇)</div>
本系列文章主要讲述jQuery框架的选择器(Selectors)使用方法,我将以实例方式进行讲述,以简单,全面为基础,不会涉及很深,我的学习方法:先入门,后进阶!<br>
本系列文章分为:基本篇、层次篇、简单篇、内容篇、可见性篇、属性篇、子元素篇、表单篇、表单对象属性篇共9篇文章。<br>
您对本系列文章有任何建议或意见请发送到邮箱:sjzlgt@qq.com <br>
由于是第一次写技术性系列文章,难免会出错或代码BUG,欢迎指出,在此谢过!<br>
您可以到<a href="http://jquery.com/" target="_blank">jQuery官网</a>来学习更多的有关jQuery知识。<br>
<span style="color:Red;"><strong>版权所有:白夜 <a href="http://www.cnblogs.com/bynet/" target="_blank">博客</a> 转载请保留出处和版权信息!</strong></span>

</div>

<div class="div">
<span class="xiaobiao">1. #id用法</span><br>
定义:根据给定的ID匹配一个元素。<br>
返回值:Element<br>
参数:id (String) : 用于搜索的,通过元素的 id 属性中给定的值<br>
实例:将ID为"div_red"的DIV的边框改为红色<br>
<span style="border:blue 1px solid;">代码:$("#div_red").css("border","red 2px solid"); <span class="green">//点击按钮一将执行这句代码</span></span><br>
<div id="div_red" style="width:300px; height:50px; background-color:#666;">
DIV ID="div_red"
<input type="button" id="btn_red" value="按钮一" class="button"/>
<script type="text/javascript">
$("#btn_red").click(function(){
$("#div_red").css("border","2px solid Red");
});
</script>
</div>
扩展:#id中的id可以是页面任何元素的id,如input,btuuon,div,table,span等等
</div>

<div class="div">
<span class="xiaobiao">2. element用法</span><br>
定义:根据给定的元素名匹配所有元素。<br>
返回值:Array<Element><br>
参数:element (String) : 一个用于搜索的元素。指向 DOM 节点的标签名。<br>
实例:将页面<P>标记内的文字颜色改为红色<br>
<span style="border:blue 1px solid;">代码:$("p").css("color","red");<span class="green"> //点击按钮二将执行这句代码</span></span>
<p id="p1" style="border:#000 1px solid;">P标记1 ID="p1"</p>
<p style="border:#000 1px solid;">P标记2 无ID</p>
<input type="button" id="btn_p_red" value="按钮二" class="button" /><br>
<script type="text/javascript">
$("#btn_p_red").click(function(){
$("p").css("color","red");
});
</script>
扩展:参数值可以是页面任何元素,如div,button,div,table,tr,td,p,h1,span,input
</div>

<div class="div">
<span class="xiaobiao">3. .class用法</span><br>
定义:根据给定的类(样式名称)匹配元素。<br>
返回值:Array<Element><br>
参数:class (String) 一个用以搜索的类(样式名称)。一个元素可以有多个类(样式名称),只要有一个符合就能被匹配到。<br>
实例:将页面所有引用了".red_test"样式的元素背景颜色改为蓝色<br>
<span style="border:blue 1px solid;">代码:$(".red_test").css("background-color","blue");<span class="green"> //点击按钮三将执行这句代码</span></span>
<div id="div_red_1" class="red_test">
DIV ID="div_red_1" calss="red_test"
</div>
<div id="div_red_2" style="width:200px; height:30px; border:#000 1px solid;">
DIV ID="div_red_2" 无class
</div>
<span id="span_red_1" class="red_test">
SPAN ID="span_red_1" calss="red_test"
</span><br>
<input type="text" id="txt_red_1" value='INPUT ID="txt_red_1" class="red_test"' class="red_test" /><br>
<input type="button" id="btn_red_1" value="按钮三" class="button" /><br>
<script type="text/javascript">
$("#btn_red_1").click(function(){
$(".red_test").css("background-color","blue");
});
</script>
扩展:可以看一下jQuery官网上<a href="http://docs.jquery.com/Selectors/class#class" target="_blank">Selectors/.class的实例</a>。
</div>

<div class="div">
<span class="xiaobiao">4. *用法</span><br>
定义:匹配所有元素 多用于结合上下文来搜索。<br>
返回值:Array<Element><br>
实例:查看页所有元素的数量<br>
<span style="border:blue 1px solid;">代码:$("*").length;<span class="green"> //点击按钮四将执行这句代码</span></span><br>
<input type="button" id="btn_4" value="按钮四" class="button" /><br>
<script type="text/javascript">
$("#btn_4").click(function(){
alert('页面上所有元素的数量是:'+$("*").length+"个");
});
</script>
扩展:可以看一下jQuery官网上<a href="http://docs.jquery.com/Selectors/all" target="_blank">Selectors/*的实例</a>。
</div>

<div class="div">
<span class="xiaobiao">5. selector1,selector2,selectorN用法</span><br>
定义:将每一个选择器匹配到的元素合并后一起返回。你可以指定任意多个选择器,并将匹配到的元素合并到一个结果内。<br>
返回值:Array<Element><br>
参数:selector1 (Selector) : 一个有效的选择器 selector2 (Selector) : 另一个有效的选择器 selectorN (Selector) : (可选) 任意多个有效选择器<br>
实例:将页面上所有引用名称为"li_test"样式的<Li>元素和ID为"li_red"的<Li>元素的边框改为绿色,宽度为5px<br>
<span style="border:blue 1px solid;">代码:$(".red_test,#btn_5").css("border","5px solid Green");<span class="green"> //点击按钮五将执行这句代码</span></span><br>
<ul>
<li class="li_test">Li class="li_test"</li>
<li id="li_red">Li id="li_red"</li>
<li>Li</li>
<li class="li_test">Li class="li_test"</li>
<li>Li</li>
<li>Li</li>
</ul>
<input type="button" id="btn_5" value="按钮五" class="button" />
<br>
<script type="text/javascript">
$("#btn_5").click(function(){
$(".li_test,#li_red").css("border","5px solid Green");
});
</script>
</div>

</body>
</html>




[Ctrl+A 全选 注:
如需引入外部Js需刷新才能执行]

注:因为jquery需要加载下,所以运行后请刷新一下即可看到效果。


有用  |  无用

猜你喜欢