jquery appendTo()方法的用法

來源:互聯網
上載者:User

append(content)

向每個匹配的元素內部追加內容。

這個操作與對指定的元素執行appendChild方法,將它們添加到文檔中的情況類似。

appendTo(content)

把所有匹配的元素追加到另一個、指定的元素元素集合中。

實際上,使用這個方法是顛倒了常規的$(A).append(B)的操作,即不是把B追加到A中,而是把A追加到B中。

 


<html>
  <head>
    <script type="text/網頁特效" src="js/jquery-1.3.2.js"></script>
    <style type="text/css教程">
        .changeP { font-weight: bold; color:red;}
    </style>

    <script type="text/javascript">
   $(document).ready(function(){
     $("<div><p>Hello</p></div>").appendTo("body")


   });

    </script>
  </head>
  <body>

   <form>
      <input type="radio" value="Option">
   </form>

  </body>
</html>

. appendTo(content): 將所有匹配的元素添加到另一個特定的目標元素中(加入作為最後一個子節點)    

   2.prependTo():同樣的, 將所有匹配的元素添加到另一個特定的目標元素中.(加入作為第一個子節點)

  不難發現兩者的區別了.以後,使用時要注意呦!

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("<p>data</p>").appendTo("body");

        });
       
           
       
    </script>
  </head>
  <body>
   
  </body>
</html>

向htm元素增加模板

<html>
  <head>
    <script type="text/javascript" src="js/jquery-1.3.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){

           
              $("p").appendTo( $("b") );
               
        
        });
    </script>
    <style>
      .selected { color:blue; }
     
    </style>
   
  </head>
  <body>
    <body>
          <b>asdf</b><p>asdf: </p>
    </body>
</html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.