jQuery的“$”

来源:互联网 发布:量化交易程序员 编辑:程序博客网 时间:2024/05/03 03:12

选择器:

$("h2 a") 选择<h2> 下的 a标记。

$("#id")  ID选择器

$(".class") 类别选择器

$("p:odd")  所以位于奇数行的<p>标记  :odd  :even

$("td:nth-child(1)") 所有表格行的第一个单元格

实例:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>属性选择器</title>
<style type="text/css">
<!--
.myClass{
 /* 设定某个CSS类别 */
 background-color:#005890;
 color:#4eff00;
}
-->
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
$(function(){
 //先用CSS3的选择器,然后添加样式风格
 $("li:nth-child(2)").addClass("myClass");
});
</script>
</head>
<body>
<ul>
 <li>糖醋排骨</li>
 <li>圆笼粉蒸肉</li>
 <li>泡菜鱼</li>
 <li>板栗烧鸡</li>
 <li>麻婆豆腐</li>
</ul>
</body>
</html>

$("li>a") 子选择器  返回<li>下的子元素<a>,不包括孙标记

$("a[herf$=pdf]") 选择所以超链接,并且这些超链接的herf属性是以 pdf 结尾的

 

解决window.onload函数冲突:

页面中如果同事存在多个window.onload函数,冲突问题会非常棘手,jQuery中的ready()方法很好的解决了这个问题,一个页面可以使用多个ready()方法。

 

 $(function(){
 $("table.datalist tr:nth-child(odd)").addClass("altrow");
});

 

自定义添加"$":

 

$.fn.disable = function(){
 //扩展jQuery,表单元素统一disable
 return this.each(function(){
  if(typeof this.disabled != "undefined") this.disabled = true;
 });
}
$.fn.enable = function(){
 //扩展jQuery,表单元素统一enable
 return this.each(function(){
  if(typeof this.disabled != "undefined") this.disabled = false;
 });
}

代码首先设置$.fn.disable,表明为"$"添加一个方法"disable()" ,其中"$.fn"是扩张jQuer时必须的。

 

完整实例如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>扩展jQuery</title>
<style>
<!--
form{
 border: 1px dotted #AAAAAA;
 padding: 1px 6px 1px 6px;
 margin:0px;
 font:14px Arial;
}
input{       /* 所有input标记 */
 color: #00008B; 
}
input.txt{      /* 文本框单独设置 */
 border: 1px inset #00008B;
 background-color: #ADD8E6;
}
input.btn{      /* 按钮单独设置 */
 color: #00008B;
 background-color: #ADD8E6;
 border: 1px outset #00008B;
 padding: 1px 2px 1px 2px;
}
select{
 width: 80px;
 color: #00008B;
 background-color: #ADD8E6;
 border: 1px solid #00008B;
}
textarea{
 width: 200px;
 height: 40px;
 color: #00008B;
 background-color: #ADD8E6;
 border: 1px inset #00008B;
}
-->
</style>
<script language="javascript" src="jquery.min.js"></script>
<script language="javascript">
$.fn.disable = function(){
 //扩展jQuery,表单元素统一disable
 return this.each(function(){
  if(typeof this.disabled != "undefined") this.disabled = true;
 });
}
$.fn.enable = function(){
 //扩展jQuery,表单元素统一enable
 return this.each(function(){
  if(typeof this.disabled != "undefined") this.disabled = false;
 });
}
function SwapInput(oName,oButton){
 if(oButton.value == "Disable"){
  //如果按钮的值为Disable,则调用disable()方法
  $("input[name="+oName+"]").disable();
  oButton.value = "Enable"; 
 }else{
  //如果按钮的值为Eable,则调用enable()方法
  $("input[name="+oName+"]").enable();
  oButton.value = "Disable"; //然后设置按钮的值为Disable
 }
}
</script>
</head>
<body>
<form method="post" name="myForm1" action="addInfo.aspx">
<p><label for="name">请输入您的姓名:</label><br><input type="text" name="name" id="name" class="txt"></p>
<p><label for="passwd">请输入您的密码:</label><br><input type="password" name="passwd" id="passwd" class="txt"></p>
<p><label for="color">请选择你最喜欢的颜色:</label><br>
<select name="color" id="color">
 <option value="red">红</option>
 <option value="green">绿</option>
 <option value="blue">蓝</option>
 <option value="yellow">黄</option>
 <option value="cyan">青</option>
 <option value="purple">紫</option>
</select></p>
<p>请选择你的性别:<br>
 <input type="radio" name="sex" id="male" value="male"><label for="male">男</label><br>
 <input type="radio" name="sex" id="female" value="female"><label for="female">女</label></p>
<p>你喜欢做些什么:
 <input type="button" name="btnSwap" id="btnSwap" value="Disable" class="btn" onclick="SwapInput('hobby',this)"><br>
 <input type="checkbox" name="hobby" id="book" value="book"><label for="book">看书</label>
 <input type="checkbox" name="hobby" id="net" value="net"><label for="net">上网</label>
 <input type="checkbox" name="hobby" id="sleep" value="sleep"><label for="sleep">睡觉</label></p>
<p><label for="comments">我要留言:</label><br><textarea name="comments" id="comments" cols="30" rows="4"></textarea></p>
<p><input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" class="btn">
<input type="reset" name="btnReset" id="btnReset" value="Reset" class="btn"></p>
</form>
</body>
</html>

 

原创粉丝点击