【发布时间】:2012-11-16 17:58:38
【问题描述】:
我正在尝试从 XML 文件中读取设置。我想我没有正确加载对象,或者我的选择器没有按照我的想法做。 appendImages 函数内部的日志消息不执行,我不知道为什么。
$(document).ready(function() {
$.ajax({
type: "GET",
url: "banner_slider/settings.xml",
dataType: "xml",
success: startSlider
});
});
function startSlider(xml) {
var bWidth = $('#banner').width(), bHeight = $('#banner').height();
bWidth += 'px';
bHeight += 'px';
$('#banner').attr( 'style', 'height: '+bHeight );
$('#banner').attr( 'style', 'width: '+bWidth );
$('#banner img').attr( 'id', 'origImg');
appendImages( bWidth, bHeight, xml );
$('#origImg').remove();
$('#banner').cycle();
}
function appendImages( bWidth, bHeight, xml ) {
console.log('appendImages executed');
$(xml).find('img').each(function() {
var path = $(this).text();
console.log('path: '+path);
$('#banner').append('<img width="'+bWidth+'" height="'+bHeight+'" src="'+path+'" />');
});
}
XML 示例:
<?xml version="1.0" encoding="utf-8" ?>
<images>
<img>test1</img>
<img>test2</img>
<img>test3</img>
</images>
【问题讨论】:
-
你的xml无效,最后一个
<images>应该是</images>。 -
谢谢,我修复了无效的 XML (face+palm)。但是,我仍然没有进入 XML 选择器。我在原始问题中替换了
appendImages中的代码。console.log('appendImages executed');正确返回控制台,console.log('path: '+path);不返回任何内容到控制台。 -
您没有在
#banner中为您的宽度和高度样式设置任何单位 -
添加了单位,但是无论是否有单位,我仍然没有正确地从 XML 文件中提取信息。
-
您的代码似乎运行良好,请参阅jsfiddle.net/mowglisanu/yQLx6