這篇文章主要介紹了jquery中append()與appendto()用法分析,以實(shí)例的形式分析了jquery中append()與appendto()的具體語法與詳細(xì)用法,需要的朋友可以參考下
本文實(shí)例分析了jquery中append()與appendto()的用法。分享給大家供大家參考。具體分析如下:
在jQuery的文檔操作方法中,append()和appentto()方法執(zhí)行的任務(wù)相同,但是兩者也有區(qū)別。
1、append()方法:在被選元素的結(jié)尾(但仍在元素內(nèi)部)插入指定的內(nèi)容。
a、語法:
復(fù)制代碼代碼如下:
$(selector).append(content);
其中,參數(shù)content是必需的,指定要附加的內(nèi)容。
b、append能夠使用函數(shù)給被選元素附加內(nèi)容,語法為:
復(fù)制代碼代碼如下:
$(selector).append(function(index,html));
其中,function()是必需的,參數(shù)index和html都是可選的。index表示接收選擇器的index位置,html表示接收選擇器的當(dāng)前html。
例子:
復(fù)制代碼代碼如下:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("p").append(" <b>Hello jQuery!</b>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>在每個 p 元素的結(jié)尾添加內(nèi)容</button>
</body>
</html>
運(yùn)行結(jié)果如下:
This is a paragraph. Hello jQuery!
This is another paragraph. Hello jQuery!
2、appendto()方法:在被選元素的結(jié)尾(但仍在元素的內(nèi)部)插入指定的內(nèi)容。但不能使用函數(shù)來附加內(nèi)容。
語法:
復(fù)制代碼代碼如下:
$(content).appendto(selector);
例子:
復(fù)制代碼代碼如下:
<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("<b> Hello jQuery!</b>").appendTo("p");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>在每個 p 元素的結(jié)尾添加內(nèi)容</button>
</body>
</html>
運(yùn)行結(jié)果如下:
This is a paragraph. Hello jQuery!
This is another paragraph. Hello jQuery!
希望本文所述對大家的jQuery程序設(shè)計(jì)有所幫助。