以下jQuery從XML數據創建變量並將其置於標記中。該變量可以在<button>
和</button>
之間使用,但是當我嘗試使用該變量爲value
屬性(或其他任何事情)創建參數時,我不能。我的語法有什麼問題。變量XML作爲通過jQuery與jQuery進行屬性的參數
$.ajax({
type: "GET",
url: "Administration/data/people.xml"
}).done(function (xml) {
$(xml).find('fullName').each(function() {
var fullName = $(this);
$('<button></button>').attr('value', fullName).html(fullName).appendTo('#loadMe');
});
}).fail(function (response, error) {
$('#info').text('Error!!');
});
開發檢查顯示:
<button value="[object] [Object]"><childnode>fullName</childnode></button>
但是,相反,我要的是:
<button value="fullName">fullName</button>
對象的默認字符串表示形式是'[object Object]',所以這就是你所看到的。 JavaScript不知道如何將對象表示爲字符串。你必須從中提取你想要的信息。簡化示例:'var obj = {foo:42};警報(OBJ);警報(obj.foo);'。 –