jQuery對P標籤的操作的幾個小執行個體講解

來源:互聯網
上載者:User

本文我們分享幾個使用jQuery操作p標籤的幾個執行個體,執行個體包括:根據id擷取元素本身(Object對象)的方法,jquery為每個p標籤增加onclick方法,如何為jquery動態產生的p標籤添加動態常值內容?



註:以下代碼均通過W3School線上測試。

根據id擷取元素本身(Object對象)的方法:

$("#id");

例如:

<!DOCTYPE html>  <html>  <head>  <script src="/jquery/jquery-1.11.1.min.js">  </script>  <script>  $(document).ready(function(){    $("p").click(function(){      alert($("#p1"));    });  });  </script>  </head>  <body>  <p id="p1" name="name1">如果您點擊我,我會消失。</p>  <p id="p2" name="name2">點擊我,我會消失。</p>  <p id="p3" name="name3">也要點擊我哦。</p>  </body>  </html>



jquery為每個p標籤增加onclick方法

<script src="../../scripts/jquery-1.3.1.js" type="text/javascript"></script>  <script type="text/javascript" >      $(function(){          $("p").click(function(){              alert("我被點擊了");          });      });  </script>  </head>  <body>  <p>測試1</p>  <p>測試2</p>



如何為jquery動態產生的p標籤添加動態常值內容?

回答:

$("<p/>").appendTo("body").html("這是P標籤的內容");


上面的代碼的意思是在body尾部追加一個P標籤,該P標籤的HTML內容是 這是P標籤的內容

聯繫我們

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