jquery通過id獲取元素 jquery怎么添加元素?
jquery怎么添加元素?使用jQuery創(chuàng)建新元素的方法是$(HTML標(biāo)記),例如,$(“<P></P>”)創(chuàng)建段落。注意,只創(chuàng)建了對象,沒有添加到文檔節(jié)點;以下四種jQuery方法
jquery怎么添加元素?
使用jQuery創(chuàng)建新元素的方法是$(HTML標(biāo)記),例如,$(“<P></P>”)創(chuàng)建段落。注意,只創(chuàng)建了對象,沒有添加到文檔節(jié)點;以下四種jQuery方法可以向文檔樹中添加新內(nèi)容:
append()-在所選元素的末尾插入內(nèi)容
prepend()-在所選元素的開頭插入內(nèi)容
after()-在所選元素的后面插入內(nèi)容
before()-在所選元素的前面插入內(nèi)容下面的示例顯示:單擊“添加”按鈕創(chuàng)建一個新的輸入文本框
1。HTML結(jié)構(gòu)
<input type=“text”id=“testuinput”><input type=“button”value=“add”/>
2。JavaScript代碼
$(function(){T/T$(“input[type=”button“]”)。單擊(function(){T/tnew)uj=$(“<input type=”text“>”)TT$(this).before(newuj][T})
3。顯示效果
初始樣式
兩次單擊“添加”按鈕后的效果
append(),appendto()add child element(end)prepend()prependto()add child element(front)insertbefore(),before()add horizontal element(front)insertafter(),after()add horizontal element(back)replacewith()和replaceall(),用于替換一個節(jié)點。Remove()刪除元素。Empty()刪除子元素