jQuery 可用字串與屬性物件快速建立新節點,再透過 html()、text() 或 append() 設定內容與插入位置。
var objNewDiv = $('<div>', { 'id': 'mydiv', 'class': 'menu' }); objNewDiv.html(123456); $('#hello').append(objNewDiv);
這種寫法比反覆拼接 HTML 字串更清楚,也能避免部分引號與標籤嵌套問題,適合動態建立表單、卡片與提示層。