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
Javascript lookback 1 folder to get image of parent Directory
提问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.php
it 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:
关键是要记住浏览器对层次结构的看法是什么:
Assuming
localhost/mydomain
is the rootof your site, then:...a page like
localhost/mydomain/somepage.php
:will appear at e.g.
http://example.com/somepage.php
from the browser's POVand can use
pics/somepic.jpg
or/pics/somepic.jpg
for the pic.
...a page like
localhost/mydomain/somepages/somepage.php
:will appear at e.g.
http://example.com/somepages/somepage.php
from the browser's POVand can use
../pics/somepic.jpg
or/pics/somepic.jpg
for the pic.
Assuming
localhost
is the root of your site instead, then:...a page like
localhost/mydomain/somepage.php
will appear at e.g.,
http://example.com/mydomain/somepage.php
from the browser's POVand can use
pics/somepic.jpg
or/mydomain/pics/somepic.jpg
for the pic.
...a page like
localhost/mydomain/somepages/somepage.php
will appear at e.g.,
http://example.com/mydomain/somepages/somepage.php
from the browser's POVand can use
../../pics/somepic.jpg
or/mydomain/pics/somepic.jpg
for the pic.
假设
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
用于图片。
假设
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
用于图片。