javascript - 获取 XML 项目的完整内容?

标签 javascript jquery html xml

我有一个 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/

相关文章:

javascript - 抓取 :after content

javascript - 在文本输入更改后运行的 AngularJS 指令

javascript - 如何实现 3 个函数的 Javascript Promise?

javascript - 使用 HTML5 拍摄 Flash 对象的屏幕截图

javascript - 如何重定向到文件

javascript - 加载后播放 HTML5 视频

javascript - 使用 jQuery 在 javascript 中解析 JSON

html - 如何更改可点击图像的 ol 元素符号

javascript - jQuery悬停()不触发

javascript - Django 无法在设置中导入 ckeditor 但可以在 shell 中导入?