Html 如何设置 img "src" 路径,以便根页面和嵌套页面都可以找到它...?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14894479/
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-08-29 05:38:27  来源:igfitidea点击:

How to set img "src" path so that root and nested pages can both find it...?

htmlpathrelative-pathsrc

提问by Vince

I'm having trouble with an <img>element in a master page that I can't get to be reachable from both the home default.aspxpage, and any nested page using the same srcpath.

<img>无法从主页default.aspx和使用相同src路径的任何嵌套页面访问母版页中的元素。

For example, from the home page (located on the root), I'm able to display the image with:

例如,从主页(位于根目录),我可以显示图像:

<img src="Images/test.jpg">

But all pages located one level down, in a folder on the root, must use src="../Images/test.jpg"to display the image.

但是位于根目录下一层文件夹中的所有页面都必须用于src="../Images/test.jpg"显示图像。

I have tried using ~and other ways to get one path to work for both locations, but no luck yet.

我已经尝试使用~和其他方法来为两个位置找到一条工作路径,但还没有运气。

This seems too easy, so I must be making this more difficult than it is... Feel free to enlighten me with any clues.

这似乎太容易了,所以我必须让这比它更难......请随时为我提供任何线索。

回答by Venkat Kotra

You can use

您可以使用

<img src="/Images/test.jpg"> 

in all the pages.

在所有页面中。

What ever be the page, image will be brought from url www.yourdomian.com/Images/test.jpg

页面是什么,图像将从 url 中获取 www.yourdomian.com/Images/test.jpg

回答by RPSarathi

Consider:

考虑:

@{    
   var src == Url.Content("~/images")+"/myImage.jpg";  
 }

Which also helps me to resolve the path ../and ../../in jquery calendar image display in IE.

这也有助于我解决路径../../../在 IE 中的 jquery 日历图像显示。

回答by nevo

In my case - c# asp.net mvc (before publishing) - I worked this workaround as also no src combination did work for me.

在我的情况下 - c# asp.net mvc(发布之前) - 我使用了这个解决方法,因为也没有 src 组合对我有用。

Somewhere on the beginning of my View

在我的视图开始的某个地方

@{
  var path = Server.MapPath("~/Images");
  var src = path + "\myImage.jpg";  
 }

Later (still on the View) for the image

稍后(仍在视图上)用于图像

<img src="@src" />

Hope this helps someone :)

希望这对某人有所帮助:)