繁体   English   中英

JavaScript 相对路径

[英]Javascript relative path

在第一个 html 文件中,我使用变量categoryLinks

var categoryLinks = {
    'Career prospects':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim01.html',
    'Compensation':'http://localhost/Landa/DirectManagers/511-HelenaChechik/Dim02.html',....

在第二个 html 文件中,我通过拉一些 Json 创建一个图表:

$(function () {
    var chart1;
    $.get('graphdata/Dim1.txt?x='+microtime(), function(json){
    obj = eval('({'+json+'})');

在这个 Json 中有一行引用categoryLinks以指向相关的超链接:

labels: {
formatter: function () {
return '<a href="' + categoryLinks[this.value] + '">' +
this.value + '</a>';
},
style: {color: 'blue',textDecoration: 'underline',fontSize:'13px'} 
}

},

一切正常

我试图将categoryLinks 中的链接定义为相对路径而不是绝对路径。 我试过不成功

var categoryLinks = {
    'Career prospects':'Landa/DirectManagers/511-HelenaChechik/Dim01.html',

这取决于您将它们加载到的页面的 URL,您尚未显示,但您可能需要一个前导/

var categoryLinks = {
    'Career prospects':'/Landa/DirectManagers/511-HelenaChechik/Dim01.html'
// Here ----------------^

...或者可能是领先的../

var categoryLinks = {
    'Career prospects':'../Landa/DirectManagers/511-HelenaChechik/Dim01.html'
// Here ----------------^^^

..或类似的。

基本上:

说你有

http://example.com/foo/page.html

你有一个相对链接:

testing/stuff.html

这将替换page.html ,为您提供:

http://example.com/foo/testing/stuff.html

如果您不想在foo ,则可以使用前导/表示“从域的根开始”:

/testing/stuff.html

... 或..意思是“上升一个级别”:

../testing/stuff.html

..可以多次使用,所以如果你有这个页面:

http://example.com/this/is/deeply/nested.html

相对链接

../../test.html

给你

http://example.com/this/test.html

另请注意,链接将相对于它们所在的 HTML 页面,而不是它们所在的 JavaScript 脚本文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM