我想优化我的代码在这里找到: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