[在html和javascript中设置文件src之间有什么区别

问题描述 投票:0回答:1

快速版本:

如果我做一个div,并在其中放一张图片,它就很好了。

this 
<img id="main_image"; src="~/images/tea_offer.jpg" style="width:100%; height:100%" /> 
gives 
localhost/images/tea_offer.jpg

但是后来我尝试使用javascript更改图像

this
document.getElementById("main_image").src = "images/cake.jpg"
gives
localhost/home/~/images/cake.jpg

并且图像不起作用。我认为这====> localhost / home /〜/ images / cake.jpg。是错的。请帮助。

长版。

我有一个简单的网站,它有一个背景图片和一个蓝色框。单击蓝色框时,背景图像会更改。图像存储在本地的wwwroot中。单击蓝色框时,背景图像只是变成白色,而我的计算机无法找到它要寻找的图像。

proof the images are stored in propper place with propper name

如何获取javascript以查找类似于html的图像?

javascript html razor view
1个回答
0
投票
而不是在JavaScript版本的路径中添加“〜”,而应使用“ /”,使其成为“ /images/cake.jpg”,以便从根目录进行查询。

更多信息:https://coderwall.com/p/8nhqeg/relative-paths-from-the-root-in-javascript

© www.soinside.com 2019 - 2024. All rights reserved.