我想優化我的代碼在這裏找到:http://codepen.io/leongaban/pen/fJGie如何修改jQuery中的HTML變量?
目前我有var inputphone
其中包含輸入字段的默認HTML。
var inputphone = $("<li><label></label><br/><input type='tel' pattern='[0-9]{10}'
class='added_phone' name='' value='' autocomplete='off' maxlength='20' /></li>");
從下拉菜單中選擇我場有幾種類型,當用戶選擇某一個領域我基本上每重複使用默認的HTML用戶可以從(移動,工作等)選擇在那一刻時間。
有沒有我可以例如只是把默認inputphone HTML到像MOBILE_PHONE另一個變量,然後插入一個字符串到標籤,或到姓名字段的方式?
東西有點像
mobile_phone.label = "Mobile Phone"
或mobile_phone.html.find('label')
?
當前代碼:
// Default Phone Input
var inputphone = $("<li><label></label><br/><input type='tel' pattern='[0-9]{10}'
class='added_phone' name='' value='' autocomplete='off' maxlength='20' /></li>");
// Create new input for Mobile Phone
var mobile_phone = inputphone;
mobile_phone.html("<li><label>Mobile Phone</label><br/><input type='tel' pattern='[0-9]{10}'
class='added_phone' name='mobile phone' value='' autocomplete='off' maxlength='20' /></li>");
// Add the Mobile Phone input to the page
$('.new_option').append(mobile_phone);
是的,但它如果你從字符串周圍刪除了$(),那麼你的工作會更好,這樣你只需要一個字符串。 .append會將它變成dom節點。您可以使用基本字符串操作根據需要對字符串進行更改。 –
您也可以使用'.clone()'創建HTML元素的副本,然後插入克隆。 – Jasper