我有以下包含几个元素的字符串:
'<span class="fn" id="fn1">Matthew</span>
<span class="spouse" id="spouse1">Evelyn Ross</span>
<span class="bday" id="bday1">1456</span>
<span class="wday" id="wday1"></span>
<span class="dday" id="dday1">2000</span>'
将其解析为 5 个变量的最佳方法是什么?
编辑
为清楚起见,这是完整的代码,我正在为 jEditable 创建一个自定义输入类型,它允许我内联编辑 vcard。
$.editable.addInputType('person', {
element : function(settings, original) {
var fn = $('<input id="fn_"/>');
var bday = $('<input id="bday_" />');
var wday = $('<input id="wday_" />');
var dday = $('<input id="dday_" />');
var spouse = $('<input id="spouse_" />');
$(this).append(fn);
$(this).append(bday);
$(this).append(wday);
$(this).append(dday);
$(this).append(spouse);
/* Hidden input to store value which is submitted to server. */
var hidden = $('<input type="hidden">');
$(this).append(hidden);
return(hidden);
},
content : function(string, settings, original) {
var $data = $(string);
var data = {};
$data.each(function () {
var $t = $(this);
data[$t.attr('id')] = {
class: $t.attr('class'),
value: $t.text()};
});
alert(data.length());
$("#fn_", this).val('Name');
$("#bday_", this).val('Born');
$("#wday_", this).val('Married');
$("#dday_", this).val('Died');
$("#spouse_", this).val('Spouse');
},
submit: function (settings, original) {
var value = "<span class=fn>" + $("#fn_").val() + '</span>' + '<span class=spouse>' + $("#spouse_").val() + '</span>' + '<span class=bday>' + $("#bday_").val() + '</span>' + '<span class=wday>' + $("#wday_").val() + '</span>' +'<span class=dday>' + $("#dday_").val() + '</span>';
$("input", this).val(value);
}
});