簡體   English   中英

無法通過 jquery / javascript 設置圖像 src

[英]Can not set image src via jquery / javascript

我有一個需要通過 jquery 或 javascript 動態設置的圖像

<img id="myImage" src="" />

我的視圖名為“UserProfile”我的控制器名稱是“User”生成的 URL 是: https://localhost/Carpool.Web/User/UserProfile

我的圖片位於 wwwroot/images 文件夾下

現在,當我使用以下任何一項時,我得到 404 並且不顯示圖像:

$("#myImage").attr({ "src": "/wwwroot/images/info.png" }); //console => <img id="myImage" src="https://localhost/Carpool.Web/User/UserProfile/wwwroot/images/info.png">
$("#myImage").attr({ "src": "~/images/info.png" });//console => <img id="myImage" src="~/images/info.png">
$("#myImage").attr({ "src": "/images/info.png" });//console => <img id="myImage" src="https://localhost/Carpool.Web/User/UserProfile/images/info.png">
$("#myImage").attr({ "src": "images/info.png" }); //console => <img id="myImage" src="https://localhost/images/info.png">
document.getElementById("myImage").src // tried all as above 

如果我的視圖是“索引”,那么我沒有這個問題,也可以毫無問題地顯示站點徽標 這與我升級到 asp.net core 3.1 相關嗎?

我需要看看你的樹結構來糾正它。

放 '。' 或“..”在路徑前面

例如

"src": "./wwwroot/images/info.png" // 僅當圖像文件夾與您的頁面具有相同的父文件夾時

"src": ../wwwroot/images/info.png"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM