2010-11-17 120 views
1

我想從flickr rss獲取圖像,但此代碼不會獲取它們。 這就是我在這裏得到一些幫助後所得到的結果:Jquery in Internet Explorer: find image problem(works in FF and Chrome)從另一個域的rss獲取圖像(然後附加它)

問題是以正確的格式獲取數據到我的網頁。 我的網址是:zalastax.co.cc/pictures.html

的Javascript:

$.ajax({ 
    type: "GET", 
    url: "js/getflickreasy.php", 
    dataType: "xml", 
    success: function(data) { 
     $(data).find("item").each(function() { 

      var item = $(this), title, description, thumbnail; 

      title = item.find("title").text(); 
      description = item.find("description").text(); 
      thumbnail = item.find("img").attr("src"); 
     }); 
    } 
}); 

PHP:

<? 
header("content-type: text/xml"); 
readfile("http://api.flickr.com/services/feeds/[email protected]&lang=en-us&format=xml"); 
?> 

編輯:馬修斯代碼工作。
但我有一個附加一些數據的問題。

var currentImage = 0; 
//get flicker images from rss. 
$.getJSON("http://api.flickr.com/services/feeds/[email protected]&lang=en-us&format=json&jsoncallback=?", function(data) { 
    $(data.items).each(function() { 
     var item = this, 
      title, description, thumbnail; 
     title = item.title; 
     description = item.description; 
     thumbnail = item.media.m; 
     if (currentImage % 3 === 0) { 
      $("#apa").append("<p>HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH</p>"); 
     } 
     currentImage++; 
     $("#apa").append("<div id=\"div" + currentImage + "\" class=\"imageContainer pictDiv\"><img id=\"img" + currentImage + "\" class=\"bild pictImg\" src=\"" + thumbnail + "\" /></div>"); 
     $("#bakgrund").append("<img id=\"bkg" + currentImage + "\" class=\"bgrund pictBkg\" src=\"Bilder/polaroid.png\" />"); 
    }); 

    $("#bakgrund").append("<img id=\"bkg" + currentImage + "\" class=\"bgrund pictBkg\" src=\"Bilder/polaroid.png\" />"); 
}); 

此代碼適用於Google Chrome。在Firefox中,它檢索數據但不做附加。 如果我嘗試追加其他內容,如簡單文本,Firefox不會追加。

它在我通過jsfiddle.net嘗試它時起作用,但它通過我自己的網頁嘗試時不起作用。

+0

這對我在Firefox中的工作正常 - http://jsfiddle.net/tBAXV/ – Sam 2010-11-18 17:21:42

+0

我知道,它通過小提琴使用它,但它在我的網頁中使用它時不起作用。我嘗試了全部源代碼,只有這個源代碼,它不會追加。在http://www.zalastax.co.cc/pictures.html查看它。 – Zalastax 2010-11-18 19:56:23

回答

1

你可以這樣做沒有PHP,使用JSONP:

$.getJSON("http://api.flickr.com/services/feeds/[email protected]&lang=en-us&format=json&jsoncallback=?", function(data) { 
     $(data.items).each(function() { 
      var item = this, title, description, thumbnail; 
      title = item.title; 
      description = item.description;; 
      thumbnail = item.media.m; 
     }); 
    }); 

我做了一個的jsfiddle demo

+0

當你在jsFiddle中使用它時,你的代碼可以正常工作,但是當我在Firefox上嘗試http://zalastax.co.cc/pictures.html並且IE它不會創建圖像。它會在使用Google Chrome時執行。 – Zalastax 2010-11-19 19:23:44

+0

@Zalastax,DOM尚未加載。您需要在['ready'](http://api.jquery.com/ready/)或您現有的'load'處理程序中執行此操作。 – 2010-11-19 23:22:16

+0

LoL。我在之前的代碼版本中使用過它,但顯然它在調試道路上丟失了某處。你知道任何一個網站,你可以學習如何熟練的調試,或者只是想知道這種語言真的很好嗎? – Zalastax 2010-11-20 01:55:16

相關問題