こんにちは、私はAJAXとxmlで作業していて、flickrアプリケーションで作業しようとしていますflickr xmlドキュメントの特定の要素を取得するには、このようにしますvar title = dataInfo.getElementsByTagName('title')[0];
私はfotoのタイトルを取得する必要があります私はFlickrのから取得することを、私はこれを行うなぜ[オブジェクト要素]を取得するのですか?
$('#results').append("<img src ="+src+" width="+width+" height="+height+">"+title);
私のタイトルは、HTMLでのタイトルのように表示さdoesntの理由、[object element]
のですか?ここ
は完全なコードです:(basicly私は(最初に私のhtmlのボタンに基づいてサイズを取得するために第2の写真集の数を、取得するために、第三の)写真の情報である3 AJAX要求を持っている:
$(document).ready(function() {
var numero = 10;
var clicked = 1;
$("#sq").click(function(){
clicked = 1;
});
$("#lg-sq").click(function(){
clicked = 2;
});
$("#thumb").click(function(){
clicked = 3;
});
$("#small").click(function(){
clicked = 4;
});
$("#mid").click(function(){
clicked = 5;
});
$("#apagar").click(function() {
$("#results").html('');
});
$('#pesquisar').click(function() {
$("#results").html('');
$.ajax({
url: 'https://api.flickr.com/services/rest/?method=flickr.photos.search',
dataType: 'xml',
data: {
api_key: '2fd41b49fedfd589dc265350521ab539',
tags: $("#tag").val(),
format: 'rest'
},
success: sucessHandler,
error: errorHandler
});
function sucessHandler(data) {
$("#results").html('');
var fotos = Array.prototype.slice.call($(data).find("photo"));
if ($("#numero").val() != "") {
numero = parseInt($("#numero").val());
console.log("entrou");
}
fotos.forEach(function(foto,key) {
if(key < numero){
$.ajax({
url: 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes',
dataType: 'xml',
data: {
api_key: '2fd41b49fedfd589dc265350521ab539',
photo_id: $(foto).attr('id'),
format: 'rest'
},
success: function(dataSize){
var farmId = $(foto).attr('farm');
var serverId= $(foto).attr('server');
var Id = $(foto).attr('id');
var secret = $(foto).attr('secret');
var src = "https://farm" + farmId + ".staticflickr.com/"+ serverId +"/" + Id + "_"+secret+".jpg";
$.ajax({
url: 'https://api.flickr.com/services/rest/?method=flickr.photos.getInfo',
dataType: 'xml',
data: {
api_key: '2fd41b49fedfd589dc265350521ab539',
photo_id: $(foto).attr('id'),
format: 'rest',
secret: secret
},
success: function(dataInfo){
if(clicked == 1){
var size = dataSize.getElementsByTagName('size')[0];
var title = dataInfo.getElementsByTagName('title')[0];
console.log(title);
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">"+title);
}
if(clicked == 2){
var size = dataSize.getElementsByTagName('size')[1];
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">");
}
if(clicked == 3){
var size = dataSize.getElementsByTagName('size')[2]
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">");
}
if(clicked == 4){
var size = dataSize.getElementsByTagName('size')[3]
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">");
}
if(clicked == 5){
var size = dataSize.getElementsByTagName('size')[4]
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">");
}
},
error: function(req,status,err){
}
});
},
error: errorSize
});
}
});
function errorSize(req, status, err) {
console.log("error size");
}
}
function errorHandler(req, status, err) {
console.log("fail");
}
});
});
関連部分:
if(clicked == 1){
var size = dataSize.getElementsByTagName('size')[0];
var title = dataInfo.getElementsByTagName('title')[0];
console.log(title);
var width = $(size).attr("width");
var height = $(size).attr("height");
$('#results').append("<img src ="+src+" width="+width+" height="+height+">"+title);
文字列ではなく、DOMElementを追加しているからです。たとえば、 'var title = dataInfo.getElementsByTagName( 'title')[0] .innerText;' –
@RoryMcCrossan 'innerText'のような要素のプロパティにアクセスしたいと思うかもしれません。代わりに 'textContent'を使用してください。 –
また、 'clicked'変数を使用してコードを大量にDRYすることで、N個の重複した' if'文を持たずに 'size'要素にインデックスでアクセスすることができます。 –