jQuery使用手册之一

2020-05-23 06:10:43易采站长站整理

说明:复制一个jQuery对象,
参数:obj (jQuery): 要复制的jQuery对象
例子:
未执行jQuery前:
<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元素的内容。
each(fn)
说明:将函数作用于所有匹配的对象上
参数:fn (Function): 需要执行的函数
例子:
未执行jQuery前:
<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。
eq(pos)
说明:减少匹配对象到一个单独得dom元素
参数:pos (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:
<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”).eq(1).html())
}运行:当点击id为test的元素时,alert对话框显示:So is this,即第二个<p>标签的内容

get() get(num)
说明:获取匹配元素,get(num)返回匹配元素中的某一个元素
参数:get (Number): 期望限制的索引,从0 开始
例子:
未执行jQuery前:
<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>标签的内容
注意get和eq的区别,eq返回的是jQuery对象,get返回的是所匹配的dom对象,所有取$(“p”).eq(1)对象的内容用jQuery方法html(),而取$(“p”).get(1)的内容用innerHTML