jQuery User Manual (2)_jquery
May 16, 2016 pm 06:45 PM二:DOM操作
属性
我们以<img id="a" scr="5.jpg"/>为例,在原始的javascript里面可以用var o=document.getElementById('a')取的id为a的节点对象,在用o.src来取得或修改该节点的scr属性,在jQuery 里$("#a")将得到jQuery对象[ <img id="a" scr="5.jpg"/> ],然后可以用jQuery提供的很多方法来进行操作,如$("#a").scr()将得到5.jpg,$("#a").scr("1.jpg")将该对象src属性改为1,jpg。下面我们来讲jQuery提供的众多jQuery方法,方便大家快速对DOM对象进行操作
herf() herf(val)
说明:对jQuery对象属性herf的操作。
例子:
未执行jQuery前
jQuery代码及功能:
alert($("#test").href());
$("#test").href("2.html");
}
运行:先弹出对话框显示id为test的连接url,在将其url改为2.html,当弹出对话框后会看到转向到2.html
同理,jQuery还提供类似的其他方法,大家可以分别试验一下:
herf() herf(val) html() html(val) id() id (val) name() name (val) rel() rel (val)
src() src (val) title() title (val) val() val(val)
操作
after(html) 在匹配元素后插入一段html
$("#test").after("<b>Hello</b>");
}
after(elem) after(elems) 将指定对象elem或对象组elems插入到在匹配元素后

$("a").after($("#test"));
}

append(html)在匹配元素内部,且末尾插入指定html
$("#test").append("<b>Hello</b>");
}
appendTo(expr) 与append(elem)相反

$("a"). appendTo ($("#test"));
}
clone() 复制一个jQuery对象
$("#test").clone().appendTo($("a"));
}
empty() 删除匹配对象的所有子节点





$("#test").empty();
}
insertAfter(expr) insertBefore(expr)
按照官方的解释和我的几个简单测试insertAfter(expr)相当于before(elem),insertBefore(expr)相当于after (elem)
prepend (html) prepend (elem) prepend (elems) 在匹配元素的内部且开始出插入
通过下面例子区分append(elem) appendTo(expr) prepend (elem)
<div>div</div>
<div>div</div>
</p>
div
<p id="a">p</p>
</div>
<div>div</div>
</p>
remove() 删除匹配对象
注意区分empty(),empty()移出匹配对象的子节点,remove(),移出匹配对象
wrap(htm) 将匹配对象包含在给出的html代码内
$("p").wrap("<div class='wrap'></div>");
}
wrap(elem) 将匹配对象包含在给出的对象内
<a href="#" onClick="jq()">jQuery</a>
$("p").wrap( document.getElementById('content') );
}
执行后相当于
遍历、组合
add(expr) 在原对象的基础上在附加符合指定表达式的jquery对象
<a href="#" onClick="jq()">jQuery</a>
var f=$("p").add("span");
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
add(el) 在匹配对象的基础上在附加指定的dom元素。
$("p").add(document.getElementById("a"));
add(els) 在匹配对象的基础上在附加指定的一组对象,els是一个数组。

var f=$("p").add([document.getElementById("a"), document.getElementById("b")])
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
ancestors () 一依次以匹配结点的父节点的内容为对象,根节点除外(有点不好理解,看看下面例子就明白了)
<p>one</p>
<span>
<u>two</u>
</span>
</div>
var f= $("u").ancestors();
for(var i=0;i < $(f).size();i++){
alert($(f).eq(i).html());}
}
第一个对象是以<u>的父节点的父节点(div)的内容为对象,[<p>one</p><span><u>two</u></span> ]
一般一个文档还有<body>和<html>,依次类推下去。
ancestors (expr) 在ancestors()的基础上之取符合表达式的对象
如上各例子讲var f改为var f= $("u").ancestors(“div”),则只返回一个对象:
[ <p>one</p><span><u>two</u></span> ]
children() 返回匹配对象的子介点
<div id="ch">
<span>two</span>
</div>
alert($("#ch").children().html());
}
children(expr) 返回匹配对象的子介点中符合表达式的节点
<span>two</span>
<span id="sp">three</span>
</div>
alert($("#ch").children(“#sp”).html());
}
$("#ch").children(“#sp”)过滤得到[<span id="sp">three</span> ]
parent () parent (expr)取匹配对象父节点的。参照children帮助理解
contains(str) 返回匹配对象中包含字符串str的对象

alert($("p").contains("test").html());
}
end() 结束操作,返回到匹配元素清单上操作前的状态.
filter(expr) filter(exprs) 过滤现实匹配符合表达式的对象 exprs为数组,注意添加“[ ]”
alert($("p").filter(".selected").html())
}
find(expr) 在匹配的对象中继续查找符合表达式的对象
alert($("p").find("#a").html())
}
is(expr) 判断对象是否符合表达式,返回boolen值
alert($("#a").is("p"));
}
You can use $("#a").is("div"); ("#a").is("#a") to test it more
next( ) next(expr) Returns the remaining sibling nodes of the matching object

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Detailed explanation of jQuery reference methods: Quick start guide

How to use PUT request method in jQuery?

How to remove the height attribute of an element with jQuery?

jQuery Tips: Quickly modify the text of all a tags on the page

Use jQuery to modify the text content of all a tags

How to view the user manual of DingTalk PC version - How to view the user manual of DingTalk PC version

Understand the role and application scenarios of eq in jQuery

Summary of commonly used file operation functions in PHP
