简体   繁体   English

将图像 src 设置为 javascript 中的 url

[英]Set image src to a url in javascript

I have an image in my page.我的页面中有一张图片。 I want to change it's src in a script tag.我想在脚本标签中更改它的 src。 this is code :这是代码:

<asp:Image ID="ImgSecode" runat="server" ImageUrl="~/CImage.aspx" CssClass="lblSec"/>

in page CImage , an image will create.在页面 CImage 中,将创建一个图像。 it works.有用。 but when i want to change the picture in script tag,it doesn;t work.但是当我想更改脚本标签中的图片时,它不起作用。 here is javascript code :这是javascript代码:

document.getElementById('<%= ImgSecode.ClientID %>').src = "CImage.aspx";

In addition when i set the source to an image which is in solution it works.此外,当我将源设置为解决方案中的图像时,它可以工作。

Can you help me what should i do?你能帮我做什么吗?

Assuming you use strictly JavaScript based solution you need to use SetAttribute method like this-假设您使用严格基于 JavaScript 的解决方案,您需要使用像这样的SetAttribute方法 -

document.getElementById("<%=ImgSecode.ClientID %>").setAttribute('src', '<Full Web Path of your Image>');

Or if you are open to Jquery as well the use .attr .或者,如果您也对 Jquery 开放,请使用.attr

$("[id='ImgSecode']").attr("src", "<Full Web Path of your Image>");

Also as per your question setting .aspx to src is by mistake I guess.另外根据你的问题,我猜将 .aspx 设置为 src 是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM