jQuery 使用手册(一) |
本文标签:jQuery,使用手册 一:核心部分 ![]() ![]() ![]() ![]() <p>three</p> <a href="#" id="test" onClick="jq()" >jQuery</a>
function jq(){
alert($("div > p").html()); } 运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容 function jq(){
$("<div><p>Hello</p></div>").appendTo("body"); } 运行:当点击id为test的元素时,向body中添加“<div><p>Hello</p></div>” <p>one</p>
<div> <p>two</p> </div><p>three</p> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
alert($(document).find("div > p").html()); } 运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容 function jq(){
$(document.body).background("black"); } 运行:当点击id为test的元素时,背景色变成黑色 ![]() ![]() ![]() ![]() ![]() jQuery代码及功能: function jq(){
运行:当点击id为test的元素时,隐藏form1表单中的所有元素 。$(form1.elements ).hide(); } $(fn) 说明:$(document).ready()的一个速记方式,当文档全部载入时执行函数 。可以有多个$(fn)当文档载入时,同时执行所有函数! 参数:fn (Function):当文档载入时执行的函数! 例子: $( function(){
$(document.body).background("black"); }) 运行:当文档载入时背景变成黑色,相当于onLoad 。 <p>one</p>
<div> <p>two</p> </div> <p>three</p> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
var f = $("div"); alert($(f).find("p").html()) } 运行:当点击id为test的元素时,弹出对话框文字为two,即div标签下p元素的内容 。 <img src="1.jpg"/>
<img src="1.jpg"/> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
$("img").each(function(){ this.src = "2.jpg"; }); } 运行:当点击id为test的元素时,img标签的src都变成了2.jpg 。 ![]() ![]() ![]() jQuery代码及功能: function jq(){
alert($("p").eq(1).html()) } 运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个<p>标签的内容 <p>This is just a test.</p>
<p>So is this</p> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
alert($("p").get(1).innerHTML); } 运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个<p>标签的内容 <div id="test1"></div>
<div id="test2"></div> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
alert($("div").index(document.getElementById(test1))); alert($("div").index(document.getElementById(test2))); } 运行:当点击id为test的元素时,两次弹出alert对话框分别显示0,1 <img src="test1.jpg"/>
<img src="test2.jpg"/> <a href="#" id="test" onClick="jq()">jQuery</a> jQuery代码及功能: function jq(){
alert($("img").length); } 运行:当点击id为test的元素时,弹出alert对话框显示2,表示找到两个匹配对象 |