我使用普通变量来显示图像列表。但我需要的是使用XML。谁能帮帮我吗。 var'imgs'应该是一个xml。
Jquery的
$(function() {
var imgs = ['http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg', 'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg', 'http://www.behok.ru/i/a/cat/gerbera.jpg', 'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg', 'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'];
var maximages = imgs.length; //No of Images
Slider();
setInterval(Slider, 3000);
var prevIndex = 0, prevPrevIndex = 0;
function Slider() {
$('#imageSlide').fadeOut("slow", function() {
do {
shuffleIndex = Math.floor(Math.random() * maximages);
} while(prevIndex == shuffleIndex || prevPrevIndex == shuffleIndex)
prevPrevIndex = prevIndex;
prevIndex = shuffleIndex;
$("#panel").fadeIn("slow").css('background', '#000');
$(this).attr('src', imgs[shuffleIndex]).fadeIn("slow");
});
}
});
答案 0 :(得分:2)
我假设你想要xml格式的图片网址,然后你想要解析这个xml。
<images>
<image-url>http://www.academyflorists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image-url>
<image-url>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image-url>
<image-url>http://www.behok.ru/i/a/cat/gerbera.jpg</image-url>
<image-url>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image-url>
<image-url>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image-url>
</images>
您可以通过以下方式解析它
var imgs = "<images><image-url>'http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg'</image-url><image-url>'http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg'</image-url><image-url>'http://www.behok.ru/i/a/cat/gerbera.jpg'</image-url><image-url>'http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg'</image-url><image-url>'http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg'</image-url></images>"
$xml = $.parseXML(imgs)
images = []
$($($xml).find('image-url')).each(function() { images.push($(this).text()) })
图片将包含所有图片网址的数组
答案 1 :(得分:1)
您还没有告诉我们您的xml会是什么样子,所以我将以下基本架构
放在一起<?xml version="1.0" encoding="utf-8" ?>
<images>
<image>url of image</image>
<image>url of next image</image>
<images>
如果你搜索jQuery网站,你会发现它告诉你如何使用Xml - http://api.jquery.com/jQuery.parseXML/
您需要在xml字符串上使用parseXML
并转换为jQuery对象。然后,您可以find()
个元素,遍历元素或选择元素文本和属性。以下示例显示了这一点。
$(function() {
// your image list as xml string
var xmlStr = '<?xml version="1.0" encoding="utf-8" ?><images><image>http://www.academy-florists.com/images/shop/thumbnails%5CValentines_Day_flowers.jpg</image><image>http://www.everythingbuttheprincess.com/assets/images/babies-in-bloom-fuchsia-flower_thumbnail.jpg</image><image>http://www.behok.ru/i/a/cat/gerbera.jpg</image><image>http://www.thebutterflygrove.com/images/thumbnails/0/200/200/thumbnail_flower-decor-makpk.jpg</image><image>http://gameinfestedent.com/gallery_photo/medium_image/image1322820610_MainPurpleOrchids3_1a.jpg</image></images>';
// parse the xml and produce an jQuery image object ($image) to represent the list of image elements
var xmlDoc = $.parseXML(xmlStr),
$xml = $(xmlDoc),
$image = $xml.find("image");
// loop throught your image elements and alert the url
$image.each(function() {
var imageElement = this, // this within the loop is the current element
$imageElement = $(imageElement),
imageUrl = $imageElement.text(); // use text to get the url
alert(imageUrl);
});
// use length on the jQuery object to get the element count
var maximages = $image.length;
alert(maximages);
var shuffleIndex = 3,
imageElement = $image[shuffleIndex], // index the random image that you want to use
$imageElement = $(imageElement),
imageUrl = $imageElement.text();
alert(imageUrl);
});