在 Jquery 中添加图像源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9698675/
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
Add image source in Jquery
提问by petko_stankoski
In my MVC app, the images will b in the App_Data folder. I want to give the source to my img tag in Jquery. Here is how I do it:
在我的 MVC 应用程序中,图像将位于 App_Data 文件夹中。我想在 Jquery 中为我的 img 标签提供源代码。这是我如何做到的:
var src1 = <%=Url.Content(Server.MapPath("/AppData/1.jpg"))%>
$("#imgLocation").attr("src", src1);
But it doesn't work. Why?
但它不起作用。为什么?
回答by Nitin Sawant
try following:
尝试以下操作:
var src1 = '<%=Url.Content(Server.MapPath("~/AppData/1.jpg"))%>';
$("#imgLocation").attr("src", src1);
回答by mas-designs
Because you are trying do access an HTML element which is called imgLocation
You either have to use
因为您正在尝试访问一个名为imgLocation
您必须使用的 HTML 元素
$('#imgLocation')
if your img has an IDofid="imgLocation"
- or
$('.imgLocation')
if your img has a classofclass="imgLocation"
$('#imgLocation')
如果您的 img 的ID为id="imgLocation"
- 或者
$('.imgLocation')
如果你的IMG拥有一流的class="imgLocation"