主要包括以下几部分:(1)内部插入(2)外部插入(3)包裹(4)替换(5)删除(6)赋值。那我们就开始详细地看一下。
1、内部插入:向一些元素的内部插入内容
(1)append(content) :向每个匹配的元素内部追加内容,追加到元素内部的末尾,比如
描述:
向所有段落中追加一些html标记。
html 代码:
i would like to say:
jquery 代码:
$(p).append(hello);
结果:
[ i would like to say: hello
]
(2)appendto(content) 把所有匹配的元素追加到另一个指定的元素元素集合中
描述:
把所有段落追加到id值为foo的元素中。
html 代码:
i would like to say:
jquery 代码:
$(p).appendto(div);
结果:
i would like to say:
i would like to say:
(3) prepend(content) 向每个匹配的元素内部前置内容
描述:
向所有段落中前置一些html标记代码。
html 代码:
i would like to say:
jquery 代码:
$(p).prepend(hello);
结果:
[ helloi would like to say:
]
(4) prepend() 把所有匹配的元素前置到另一个、指定的元素元素集合中。
描述:
把所有段落追加到id值为foo的元素中。
html 代码:
i would like to say:
jquery 代码:
$(p).prependto(#foo);
结果:
i would like to say:
2、内部插入:向一些元素的外部插入内容
(1)after(content)在每个匹配的元素之后插入内容。
描述:
在所有段落之后插入一些html标记代码。
html 代码:
i would like to say:
jquery 代码:
$(p).after(hello);
结果:
i would like to say:
hello
(2)before() 在每个匹配的元素之前插入内容
描述:
在所有段落之前插入一些html标记代码。
html 代码:
i would like to say:
jquery 代码:
$(p).before(hello);
结果:
[ helloi would like to say:
]
(3)insertafter 把所有匹配的元素插入到另一个、指定的元素元素集合的后面。
描述:
把所有段落插入到一个元素之后。与 $(#foo).after(p)相同
html 代码:
i would like to say:
hello
jquery 代码:
$(p).insertafter(#foo);
结果:
hello
i would like to say:
(4)insertbefore 把所有匹配的元素插入到另一个、指定的元素元素集合的前面
描述:
把所有段落插入到一个元素之前。与 $(#foo).before(p)相同。
html 代码:
hello
i would like to say:
jquery 代码:
$(p).insertbefore(#foo);
结果:
i would like to say:
hello
3、包裹:把一些元素包裹起来
(1)wrap(html) 把所有匹配的元素用其他元素的结构化标记包裹起来
描述:
把所有的段落用一个新创建的div包裹起来
html 代码:
test paragraph.
jquery 代码:
$(p).wrap(
);
结果:
test paragraph.
(2) wrap(elem) 把所有匹配的元素用其他元素的结构化标记包装起来
描述:
用id是content的div将每一个段落包裹起来
html 代码:
test paragraph.
jquery 代码:
$(p).wrap(document.getelementbyid('content'));
结果:
test paragraph.
(3)wrapall(html) 将所有匹配的元素用单个元素包裹起来
描述:
用一个生成的div将所有段落包裹起来
html 代码:
hello
cruel
world
jquery 代码:
$(p).wrapall(
);
结果:
hello
cruel
world
(4) wrapall(elem)将所有匹配的元素用单个元素包裹起来
描述:
用一个生成的div将所有段落包裹起来
html 代码:
hello
cruel
world
jquery 代码:
$(p).wrapall(document.createelement(div));
结果:
hello
cruel
world
(5) wrapinner(html)将每一个匹配的元素的子内容(包括文本节点)用一个html结构包裹起来
描述:
把所有段落内的每个子内容加粗
html 代码:
hello
cruel
world
jquery 代码:
$(p).wrapinner();
结果:
hello
cruel
world
(6) wrapinner(elem)
描述:
把所有段落内的每个子内容加粗
html 代码:
hello
cruel
world
jquery 代码:
$(p).wrapinner(document.createelement(b));
结果:
hello
cruel
world
4、替换:用制定的元素替换一些hmtl或者dom元素
(1)replacewith(content) 将所有匹配的元素替换成指定的html或dom元素
描述:
把所有的段落标记替换成加粗的标记。
html 代码:
hello
cruel
world
jquery 代码:
$(p).replacewith(paragraph. );
结果:
paragraph. paragraph. paragraph.
(2) repalceall(selector) 用匹配的元素替换掉所有 selector匹配到的元素。
描述:
把所有的段落标记替换成加粗标记
html 代码:
hello
cruel
world
jquery 代码:
$(paragraph. ).replaceall(p);
结果:
paragraph. paragraph. paragraph.
5、删除:删除制定的元素
(1)empty() 删除匹配的元素集合中所有的子节点。
描述:
把所有段落的子元素(包括文本节点)删除
html 代码:
hello, person and person
jquery 代码:
$(p).empty();
结果:
(2)remove([expr]) 从dom中删除所有匹配的元素
描述:
从dom中把所有段落删除
html 代码:
hello
how are you?
jquery 代码:
$(p).remove();
结果:
how are
描述:
从dom中把带有hello类的段落删除
html 代码:
hello
how are you?
jquery 代码:
$(p).remove(.hello);
结果:
how are you?
5、复制:克隆匹配的元素
(1)clone() 克隆匹配的dom元素并且选中这些克隆的副本。
描述:
克隆所有b元素(并选中这些克隆的副本),然后将它们前置到所有段落中。
html 代码:
hello, how are you?
jquery 代码:
$(b).clone().prependto(p);
结果:
hellohello, how are you?
(2) clone(true) 元素以及其所有的事件处理并且选中这些克隆的副本
描述:
创建一个按钮,他可以复制自己,并且他的副本也有同样功能。
html 代码:
clone me!
jquery 代码:
$(button).click(function(){
$(this).clone(true).insertafter(this);
});
终于整理完毕。以上内容参考与jquery1.3中文参考。
希望此篇对初学者有益。
