img src =的变量 [英] Variable for img src=
问题描述
我有一个问题。
首先,我不会假装我知道我在这里谈论的是什么。我是http和JavaScript的新手。
I have a question. Firstly, I am not going to pretend that I know what I am talking about here. I am a newbie to http and JavaScript.
我想我的问题可以在这篇文章中回答
IMG SRC标签和JavaScript
但我想我会解释一下我想要实现的确切方法,以防有更简单的方法。
I think my question may be answered in this post IMG SRC tags and JavaScript but I thought I would explain the exact thing I am trying to achieve in case there is an easier way.
我有一个网页,我想在其上显示图像。唯一的问题是,图像来自自动系统监视器,图像每天自动生成,并根据日期放在新目录中。
I have a webpage, I want to display an image on it. Only thing is, the image is coming from an automated system monitor, the image is automatically generated each day and placed in a new directory depending on date.
例如。 4月4日= http:// host / partition / 2009 / apr / 04 / cpu .gif
例如4月5日= http:// host / partition / 2009 / apr / 05 / cpu .gif
为了实现这一点,我创建了一些基本的JavaScript,以我需要的格式提供日期。我有这一切工作。现在我只想使用我创建的变量来显示图像。
我将JavaScript代码存储在名为displaydate()的函数中
如果我这样做< script language =JavaScript> displaydate()< / script>
我看到
http://host/partition/2009/apr/05/cpu.gif 这是正确的。
To facilitate this, I have created some basic JavaScript to give me the date in the format I need it. I have all that working. Now I just want to use that variable I created to show the image.
I have the JavaScript code stored in a function called displaydate()
If I do this <script language="JavaScript"> displaydate() </script>
I see
"http://host/partition/2009/apr/05/cpu.gif" and that is correct.
现在我如何显示这个在网站上正确吗?
Now how do I display this on the site correctly?
<a href="displaydate()"><img src="displaydate()" </a></td> //This does not work. I am just adding it to show where I have been heading.
P.S。我已经阅读了很多关于这方面的内容,并且已经尝试了很多东西,但到目前为止还没有运气。任何帮助,将不胜感激。
P.S. I have read a lot of pages on this and been trying a lot of things, but have had no luck so far. Any help, would be much appreciated.
推荐答案
是的,该页面可能确实回答了您的问题。基本上,你想要这个javascript:
Yes, that page probably does answer your question. Basically, you want this javascript:
<script type="text/javascript">
document.getElementById('image').src = "yourpicture.png";
</script>
除非您想用您编写的函数替换yourpicture.png以生成正确的路径到磁盘上的图像,所以......
Except you want to replace the "yourpicture.png" with the function you wrote to generate the correct path to the image on disk, so...
<script type="text/javascript">
document.getElementById('image').src = displaydate();
</script>
当然,您可能需要根据自己的需要对其进行修改,getElementById将采用一个参数,无论你的< id属性是什么img>标签是。您可能希望在页面加载后执行上述javascript,即:
Of course, you might need to modify this a bit for your own uses, the getElementById will take as an argument whatever the id attribute of your < img > tag is. You probably want to execute the above javascript after your page has loaded, i.e.:
<html>
<head>
<script type="text/javascript">
function load()
{
document.getElementById('image').src = displaydate();
}
function displaydate()
{
//your displaydate() function here
}
</script>
</head>
<body onload="load()">
<img src="nothing.jpg" id="image" name="image"/>
</body>
</html>
这篇关于img src =的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!