[英]How to implement "add new item" functionality in JavaScript?
標題不是很描述,但我找不到更好的。 隨意編輯它。
基本上我正在尋找的是執行以下操作的最佳方法:
當用戶單擊“添加新項目”時,將添加一個帶有相同文本框和下拉列表的新行,如上所示。 我能想到的選項如下:
<script language="html">
標簽來定義模板)。你有什么建議? 我對上述任何解決方案都不完全滿意。
如果您已經在您的頁面上使用了 jquery 或 sencha 等框架,那么您不妨使用它。
否則,我會盡可能簡單。 這看起來不是一個非常重要的核心功能,所以不要創建需要額外 https 請求甚至整個庫才能加載的東西。 克隆或生成 html 可能看起來不優雅,但它會是:
不要為像這樣微不足道的事情創造一些矯枉過正的東西。
假設超級簡單的方法並且您的格式在表格中:
<table>
<tr>
<td><input type="text" name="item_name" placeholder="item name" /></td>
<td><select name="item_type"><option value="" selected="selected">Type</option></select></td>
</tr>
<tr>
<td><input type="text" name="item_name" placeholder="item name" /></td>
<td><select name="item_type"><option value="" selected="selected">Type</option></select></td>
</tr>
<tr>
<td colspan="2" id="add">+ Add new item</td>
</tr>
</table>
您可以使用以下內容:
$('#add').on('click',function(e){
var $this = $(this);
var $newRow = $this.closest('table').find('tr:first').clone();
$newRow.find(':input').val('');
$newRow.insertBefore($this.parent());
});
分解:
$this.closest('table')
我們正在點擊的當前表格( $this.closest('table')
).clone()
).find(':input').val('')
)$newRow.insertBefore(...)
)您也可以采用模板方法,但這實際上取決於您以及您希望對輸出進行多少控制。
雖然我意識到你已經有了一個公認的答案,但我想我會提供一種簡單的 JavaScript 方法來實現相同的目標:
function closest(el, tag) {
if (!el || !tag) {
return false;
}
else {
var curTag = el.tagName.toLowerCase();
return curTag == tag.toLowerCase() && curTag !== 'body' ? el : closest(el.parentNode, tag);
}
}
function addRow(el) {
if (!el) {
return false;
}
else {
var tr = closest(el, 'tr').previousElementSibling,
newRow = tr.cloneNode(true);
tr.parentNode.insertBefore(newRow, tr.nextSibling);
}
}
document.getElementById('add').onclick = function() {
addRow(this);
}
稍微修改上面的內容,添加一個簡單的墊片來應對那些沒有實現previousElementSibling
瀏覽器:
function closest(el, tag) {
if (!el || !tag) {
return false;
}
else {
var curTag = el.tagName.toLowerCase();
return curTag == tag.toLowerCase() && curTag !== 'body' ? el : closest(el.parentNode, tag);
}
}
function prevElementSiblingShim(el) {
if (!el) {
return false;
}
else {
var prevSibling = el.previousSibling;
return prevSibling.nodeType == 1 ? prevSibling : prevElementSiblingShim(prevSibling);
}
}
function addRow(el) {
if (!el) {
return false;
}
else {
var par = closest(el, 'tr'),
tr = par.previousElementSibling || prevElementSiblingShim(par),
newRow = tr.cloneNode(true);
tr.parentNode.insertBefore(newRow, tr.nextSibling);
}
}
document.getElementById('add').onclick = function() {
addRow(this);
}
參考:
將字符串中的 HTML 插入 DOM 是最有效的方法。 除非您一次插入數百個,否則這並不重要。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.