我有一个 XML 文件,其中包含如下所示的项目:
<CutOffItem>
<partnum>SAIT22021</partnum>
<diameter>4-1/2"</diameter>
<width>0.045"</width>
<arbor>7/8"</arbor><material>METAL</material>
<maxrpm>13300</maxrpm>
<brand>UNITED ABRASIVES, INC.</brand>
<imgsrc>style\images\Original Bolt Pics\Abrasives\Cutoff\SAIT 4.5-8in.jpg</imgsrc>
</CutOffItem>
我通过 jQuery 引用每个元素以在 HTML 文档中使用,如下所示:
xmlDoc = $.parseXML( xml ),
$xml = $( xmlDoc ),
$partnum = $xml.find( "partnum" );
$title = $xml.find( "title" );
$category = $xml.find( "category" );
$type = $xml.find( "type" );
$diameter = $xml.find( "diameter" );
$width = $xml.find( "width" );
$arbor = $xml.find( "arbor" );
$brand = $xml.find( "brand" );
$imgsrc = $xml.find( "imgsrc" );
并像这样显示它们:
//Title
var h3 = document.createElement('p');
h3.innerHTML = $title.text();
$('#item').append(h3);
一切都按预期工作,但是当我尝试对 img src 执行此操作时:
// IMG
var img = document.createElement('img');
var src = document.createAttribute('src');
src.value = $imgsrc.text();
img.setAttributeNode(src);
$('#item').append(img);
在 HTML 页面代码上显示的 img 元素如下所示:
<img src="styleimagesOriginal Bolt PicsAbrasivesCutoffSAIT 4.5-8in.jpg">
.text() 删除了 URL 中的所有正斜杠。如何保留正斜杠?
最佳答案
我发现您正在使用 jQuery。对于图像,请尝试删除为此的代码,并实现以下内容:
// IMG
$("#item).append("<img src="+ $imgsrc + "title="+ $brand +"></img>");
从长远来看,图像 URL 中的间距通常不是最好的。每个空间的 URL 编码应为 %20。 http://www.w3schools.com/tags/ref_urlencode.asp
但是这是你的决定..祝你好运!!
关于javascript - 获取 XML 项目的完整内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35001524/