Javascript 回顾 1 文件夹以获取父目录的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17401858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:11:36  来源:igfitidea点击:

Javascript lookback 1 folder to get image of parent Directory

javascriptdirectoryparent

提问by user2538403

My site layout (for this example):

我的网站布局(对于这个例子):

localhost/mydomain/
-localhost/mydomain/pics
-localhost/mydomain/somepages/arandompage.php

I want to access picture from here:

我想从这里访问图片:

localhost/mydomain/index.php

I did this:

我这样做了:

<img id='4' src='pics/somepic.1.jpg'>

The mouseover snippet:

鼠标悬停片段:

{ $('#'+someId).attr('src', 'pics/somepic.'+num+'.jpg');

This is working. But when I wanted to access it from here it doesn't work. But when you go localhost/mydomain/somepages/arandompage.phpit obviously doesn't work, no longer relative.

这是有效的。但是当我想从这里访问它时它不起作用。但是当你去的时候localhost/mydomain/somepages/arandompage.php它显然不起作用,不再是相对的。

<img id='4' src='../pics/somepic.1.jpg'>

Ok here obviously that'll fix the display image. It is not working in JavaScript.

好的,显然这将修复显示图像。它不适用于 JavaScript。

{ $('#'+videoId).attr('src', '../pics/somepic.'+num+'.jpg'); /

I don't know what to do here, but yes all I need is the JavaScript to just look back 1 folder to get that img src. That's all. If you answer please insert what should be here in:

我不知道在这里做什么,但是是的,我需要的只是 JavaScript 来查看 1 个文件夹以获取该 img src。就这样。如果您回答,请插入此处应包含的内容:

`{ $('#'+videoId).attr('src', ====> 'pics/somepic.'+num+'.jpg');` 

So that it goes to the parent directory.

以便它转到父目录。

P.S. I tried absolute paths but to no avail either.

PS我尝试了绝对路径,但也无济于事。

采纳答案by T.J. Crowder

The key thing is to remember what the browser'sview of the hierarchy is:

关键是要记住浏览器对层次结构看法是什么:

  1. Assuming localhost/mydomainis the rootof your site, then:

    ...a page like localhost/mydomain/somepage.php:

    • will appear at e.g. http://example.com/somepage.phpfrom the browser's POV

    • and can use pics/somepic.jpgor /pics/somepic.jpgfor the pic.

    ...a page like localhost/mydomain/somepages/somepage.php:

    • will appear at e.g. http://example.com/somepages/somepage.phpfrom the browser's POV

    • and can use ../pics/somepic.jpgor /pics/somepic.jpgfor the pic.

  2. Assuming localhostis the root of your site instead, then:

    ...a page like localhost/mydomain/somepage.php

    • will appear at e.g., http://example.com/mydomain/somepage.phpfrom the browser's POV

    • and can use pics/somepic.jpgor /mydomain/pics/somepic.jpgfor the pic.

    ...a page like localhost/mydomain/somepages/somepage.php

    • will appear at e.g., http://example.com/mydomain/somepages/somepage.phpfrom the browser's POV

    • and can use ../../pics/somepic.jpgor /mydomain/pics/somepic.jpgfor the pic.

  1. 假设localhost/mydomain是您网站的根目录,则:

    ...一个页面,如localhost/mydomain/somepage.php

    • 将出现在例如http://example.com/somepage.php来自浏览器的 POV

    • 并且可以使用pics/somepic.jpg/pics/somepic.jpg用于图片。

    ...一个页面,如localhost/mydomain/somepages/somepage.php

    • 将出现在例如http://example.com/somepages/somepage.php来自浏览器的 POV

    • 并且可以使用../pics/somepic.jpg/pics/somepic.jpg用于图片。

  2. 假设localhost是您网站的根目录,则:

    ...一个页面 localhost/mydomain/somepage.php

    • 将出现在例如,http://example.com/mydomain/somepage.php来自浏览器的 POV

    • 并且可以使用pics/somepic.jpg/mydomain/pics/somepic.jpg用于图片。

    ...一个页面 localhost/mydomain/somepages/somepage.php

    • 将出现在例如,http://example.com/mydomain/somepages/somepage.php来自浏览器的 POV

    • 并且可以使用../../pics/somepic.jpg/mydomain/pics/somepic.jpg用于图片。