简体   繁体   English

我想使用一个javascript变量来存储图像位置

[英]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.

相关问题 我想像这样将javascript变量存储在数组中是可能的吗? - i want to store javascript variable store in array like this is possible? 将图像上传到 S3 存储桶将其存储为应用程序/八位字节流,但我想将其存储为图像以使用它(JavaScript / 节点) - Uploading Image to S3 bucket stores it as application/octet-stream, but I want to store it as an image to use it (JavaScript / Node) 我想在jsp中使用javascript进行动态循环变量 - I want to use javascript for loop variable dynamic in jsp 如何将图像选择存储到可变JavaScript - How do i store a selection of a image to variable JavaScript 我可以将图像的路径存储为变量并按如下所示使用它吗 - Can I store the path of an image as a variable and use it as given below 我想将变量数组存储为 $_SESSION 变量 - I want to store an array of variables as a $_SESSION variable 我想分配一个变量两次,但只想在 javascript 中使用第一个变量 - I want to assign one variable twice but only want to use first one in javascript 我想将文本存储在 javascript 变量中的 p 标签下,然后在输入字段中显示 - i want to store text under p tag in javascript variable then show in input field 我想将 Javascript 数组存储为 Cookie - I want to store Javascript array as a Cookie 我想将 JavaScript 变量作为 PHP 变量 - I want to JavaScript variable get as a PHP variable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM