首頁 > web前端 > js教程 > 主體

jquery中append()與appendto()用法分析

巴扎黑
發布: 2017-06-24 11:24:00
原創
1791 人瀏覽過

這篇文章主要介紹了jquery中append()與appendto()用法分析,以實例的形式分析了jquery中append()與appendto()的具體語法與詳細用法,需要的朋友可以參考下

本文實例分析了jquery中append()與appendto()的用法。分享給大家供大家參考。具體分析如下:

在jQuery的文檔操作方法中,append()和appentto()方法執行的任務相同,但兩者也有差異。

1、append()方法:在被選元素的結尾(但仍在元素內部)插入指定的內容。

a、語法:

程式碼如下:

$(selector).append(content);

其中,參數content是必要的,指定要附加的內容。

b、append能夠使用函數給被選元素附加內容,語法為:

程式碼如下:

##$(selector).append( function(index,html));

#其中,function()是必要的,參數index和html都是可選的。 index表示接收選擇器的index位置,html表示接收選擇器的目前html。

範例:


程式碼如下:

<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 元素的结尾添加内容</button>  
</body>  
</html>
登入後複製

執行結果如下:

This is a paragraph. Hello jQuery!

This is another paragraph. Hello jQuery!

2、appendto()方法:在被選元素的結尾(但仍在元素的內部)插入指定的內容。但不能使用函數來附加內容。

語法:

程式碼如下:

$(content).appendto(selector);

範例:


程式碼如下:

<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 元素的结尾添加内容</button>  
</body>  
</html>
登入後複製

運行結果如下:

This is a paragraph. Hello jQuery!

This is another paragraph. Hello jQuery!

以上是jquery中append()與appendto()用法分析的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!