简体   繁体   English

使用相对路径 - 为路径创建变量名?

[英]Working with Relative Paths - Create variable name for path?

I am working on html/javascript and found these imports prone to confusion:我正在研究 html/javascript,发现这些导入容易混淆:

  <link rel="stylesheet" href="../../../../_GUI/css/sample.css">
  <link rel="stylesheet" href="../../../../_GUI/css/button.css">

Multiple ../../ in code doesn't really look nice and looks confusing.代码中的多个../../看起来并不好看,而且看起来很混乱。

Is there any way I can make something like a variable where in:有什么办法可以让我在以下位置创建一个变量:

GUI = ../../../../_GUI ?

I also checked absolute paths but that would not be so reliable since root folder may change.我还检查了绝对路径,但这不会那么可靠,因为根文件夹可能会改变。

Help~帮助~

you can use a simple declaration like below and use that variable all over the script.您可以使用如下所示的简单声明,并在整个脚本中使用该变量。

<script type="text/javascript">
 var stylePath = 'http://_GUI/css/sample.css';
</script>

And also you can use jQuery to get the file location in runtime like below.您还可以使用 jQuery 在运行时获取文件位置,如下所示。

var fileLocation = $('script[src*=sample]').attr('src'); 
fileLocation = fileLocation.replace('sample.css', '');  

Go to this Question to learn more about this.转到此问题以了解有关此问题的更多信息。

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

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