如何在 jQuery 中建立一個 div 元素?


有許多方法可以使用 jQuery 新增一個 div,但正如需求中所述,當單擊任何方塊時,我們必須新增一個 div。 以下程式碼將幫助在單擊時新增一個 div 

Click on any square below to see the result
 <div id='box'>    
       <div class = "div" style = "background-color:blue;"></div>
       <div class = "div" style = "background-color:green;"></div>
       <div class = "div" style = "background-color:red;"></div>
</div>
$(".div").on("click",function(){
$('#box').append(
  $('<div/>')
    .attr("id", "newDiv1")
    .addClass("div")
    .append("<span/>")
      .text("hello world")
    );
});

更新於: 2020 年 6 月 12 日

801 次瀏覽

開啟你的 職業

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.