在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 11:10:27  来源:igfitidea点击:

Add image source in Jquery

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 imgLocationYou either have to use

因为您正在尝试访问一个名为imgLocation您必须使用的 HTML 元素

  1. $('#imgLocation')if your img has an IDof id="imgLocation"
  2. or $('.imgLocation')if your img has a classof class="imgLocation"
  1. $('#imgLocation')如果您的 img 的IDid="imgLocation"
  2. 或者$('.imgLocation')如果你的IMG拥有一流class="imgLocation"