为什么我得到一个[object元素]

why i get a [object element]?

本文关键字:一个 object 元素 为什么      更新时间:2023-09-26

嗨,我的工作与Ajax和xml,我试图工作与flickr应用flickr的事情是,在xml文档中获得一个特定的元素,我这样做的var title = dataInfo.getElementsByTagName('title')[0];我需要获取从flickr获取的照片的标题,当我执行

$('#results').append("<img src ="+src+" width="+width+" height="+height+">"+title); 

我的标题是[object element],为什么它不显示像一个标题在HTML?

这里是完整的代码:(基本上我有3个ajax请求(首先获得一些照片,第二获得基于我的html上的按钮的大小,第三是照片信息):

$(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);

您试过使用下列方法吗?

var title = dataInfo.getElementsByTagName('title')[0].innerText;

这将为您提供元素的文本,而不是对象本身。