[英]I want to use a javascript variable to store an image location
I'm pretty new to coding in javascript, and am currently making pop up notifications for a website in order to give feedback to users. 我刚开始使用javascript进行编码,并且目前正在向网站发出弹出通知,以向用户提供反馈。 I have searched around a bit and know there is a way to do this, but can't get my head wrapped around it.
我进行了一些搜索,并且知道有一种方法可以执行此操作,但是无法解决这个问题。 I need to store an image location to a variable in my javascript that I can reference in my html page, enabling me to output a different icon image on my toast notification based on the type that is called (error, more info, warning, ect).
我需要将图像位置存储到我的javascript页面中可在html页面中引用的javascript中的变量中,从而使我能够根据所称的类型在我的吐司通知上输出其他图标图像(错误,更多信息,警告等) )。 This is my code so far:
到目前为止,这是我的代码:
<div id="toast">
<div id ="title" style="<some styling code>"></div>
<img id ="icon" style="<image style code>"/>
</div>
And my script: 而我的脚本:
function showTypeOfToast()
{
var i = document.getElementById("toast")
i.className = "showTypeOfToast";
var label = "Type Of Toast";
document.getElementById("title").innerHTML = label
var img = "imgsrc.png";
var iconElement = document.getElementById("icon");
iconElement.src = img;
}
My title element works as I expect it to, and I am wanting the image element to work in the same way. 我的title元素可以按预期的方式工作,并且我希望image元素以相同的方式工作。
Cheers 干杯
pass a parameter into showTypeOfToast. 将参数传递给showTypeOfToast。 Pass in the type of toast you want to show.
传递您想要显示的烤面包的类型。 then inside the function define something like:
然后在函数内部定义如下内容:
if (type == warning)
iconElement.src = "warning.jpg";
else if (type == help)
iconElement.src = "help.jpg";
document.getElementById('imageId').src = 'x.jpg';
should work in any case. 在任何情况下都应该起作用。
or you can state: 或者您可以声明:
var x = "www.yoursite.com/your-image.jpg";
var y = "www.yoursite.com/your-image2.jpg";
then document.getElementById('imageId').src = x;
然后
document.getElementById('imageId').src = x;
and document.getElementById('imageId').src = y;
和
document.getElementById('imageId').src = y;
work 工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.