如何在 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 日

805 次瀏覽

開啟你的職業

完成課程並獲得認證

開始
廣告
© . All rights reserved.