jquery 元素控制(追加元素/追加內容)介紹及應用

來源:互聯網
上載者:User

jquery 元素控制(追加元素/追加內容)介紹及應用
一、在元素內部/外部追加元素 append,prepend:添加到子項目
before,after:作為兄弟元素添加

html:

  在我的後面追加一條新聞   
js:
<script type="text/javascript"> jQuery(function(){ //在元素內部追加內容 $("#content").append(" 姚明退役了... "); }) </script> 

在#content裡面添加元素,這是把

姚明退役了...

作為子項目添加到#content,如果想在元素外部追加元素,需要用到after,這樣的話就可以把

姚明退役了...

作為兄弟元素追加到#content後面。

<script type="text/javascript"> jQuery(function(){ //將span添加到追加到content1的後面 $("#content").after(" 姚明退役了... "); }) </script> 


二、在元素的不同位置追加內容

html:

  窗前明月光   疑是地上霜 
Js:
<script type="text/javascript"> jQuery(function(){ //將span添加到追加到content1的後面 $("span").appendTo("#content1"); }) </script> 

三、在元素的開始位置追加內容

html:

   在我的後面追加一條新聞   
js:
<script type="text/javascript"> jQuery(function(){ //在元素的開始位置追加內容 $("#content").prepend(" 頭部 "); }) </script> 

四、在不同元素的開始位置追加內容

html:

  在我的後面追加一條新聞   疑是地上霜 
js:
<script type="text/javascript"> jQuery(function(){ //在不同元素的開始位置追加內容 $("span").prependTo("#content"); }) </script> 

在#content裡面添加元素,這是把疑是地上霜作為子項目添加到#content開始地方,如果想在元素外部添加開始元素,需要用到before,這樣的話就可以把疑是地上霜作為兄弟元素追加到#content開始了

 <script type="text/javascript"> jQuery(function(){ //在不同元素的開始位置追加內容 $("span").before("#content"); }) </script> 

五、用指定結構的元素包含元素

Warp(html)
在指定的html中指定html元素,但指定的元素中不可含有兄弟元素,否則不能正常的運行,並且不含有一般文本字串,下面的代碼用div標籤包含p標籤。

html:

 
js:
<script type="text/javascript"> jQuery(function(){ $("span").wrap(""); }) </script> 

六、用指定的元素包含多個html元素

wrapAll(html)

html:

 
js:
<script type="text/javascript"> jQuery(function(){ $("p").wrapAll("  "); }) </script> 

七、用指定的標籤包含子項目

wrapInner(html)
與warp方法一樣,在html中指定html元素,例如:
用b標籤包含p標籤中文字的代碼如下:

html:

將p標籤的文字用b標籤包含起來

也將p標籤的文字用b標籤包含起來

js:
javascript <script type="text/javascript"> jQuery(function(){ $("p").wrapInner(""); }) </script> 



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.