我有一个xml文件,格式如下
<Products>
<Product id="1">
<ProductName>RegEB-Tote</ProductName>
<ImageFileName>ToteTank_350g.png</ImageFileName>
</Product>
<Product id="2">
<ProductName>RegEB-Drum</ProductName>
<ImageFileName>N/A</ImageFileName>
</Product>
<Product id="3">
<ProductName>RegEB-5Gal</ProductName>
<ImageFileName>5Gallon.png </ImageFileName>
</Product>
<Product id="4">
<ProductName>RegEB-Gal</ProductName>
<ImageFileName>GalSqueezeBottle.png</ImageFileName>
</Product>
我试图根据Products id属性值获取imagefilename,但是我得到了xml处理错误
$(document).ready(function () {
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
var prodid = getParameterByName("ID");
var fieldheight = ($('#fld_productDetails').height());
$.ajax({
type: "GET",
url: "OtherDataFiles/ProductImageLookup.xml",
dataType: "xml",
success: function (xml) {
$(xml).find('Product').each(function () {
if ($(this).attr('id').value === prodid)
{
var filename = $(this).find('ImageFileName').text();
$('#fld_productDetailImage').css({ 'height': fieldheight,'url': "../Images/ProductImages/" + filename + "no-repeat center" });
}
});
},
error: function() {
alert("An error occurred while processing XML file.");
}
});
});
prodid和fieldheight变量正在获得正确的值。变量文件名不是
答案 0 :(得分:0)
$(xml).find的if部分不正确,添加后台css的语法错误。正确的代码如下。
if ($(this).attr('id') === prodid) {
var filename = $(this).find('ImageFileName').text();
$('#fld_productDetailImage').css({ 'height': fieldheight, 'background': "url(../Images/ProductImages/" + filename + ") no-repeat center" });
}