【发布时间】:2013-06-28 23:00:24
【问题描述】:
我在 HTML 中使用 JavaScript 从几个变量创建 HREF 链接。 第一个变量是 Android 文件路径开头的固定文本(每次都相同),第二个变量是取自 XML 属性的文件名。 这一切都很好,但是如果我在 XML 文档的文件名变量中没有任何空格,它只会正确构建链接。 基本上发生的情况是,如果文件名变量包含一个空格,它只会构造链接直到文件名中的第一个空格,所以一个例子是
**Correct link =**
<a href="file:///sdcard/Clients/PB/example file name.pdf">example file name.pdf</a>
**Link my code incorrectly returns =**
<a href="file:///sdcard/Clients/PB/example">example file name.pdf</a>
我们将不胜感激。 谢谢!
<script>
xmlDoc=loadXMLDoc("PBFileNames.xml");
x=xmlDoc.getElementsByTagName("file");
var path = "file:///sdcard/Clients/PB/"; //this will be constant between all iterations
for (i=0;i<x.length;i++)
{
var filename = x[i].getAttributeNode("name").nodeValue; //the nodefile is the filename
{
document.write("<br>");
document.write("<a href=" + path + filename + ">" + filename + "</a>");
document.write("<br>");
}
}
</script>
【问题讨论】:
-
将文件名中的空格替换为
%20,然后重试。
标签: javascript html regex uri space