Javascript - 在两个图像之间切换 [英] Javascript - Switching Between Two Images
问题描述
< head runat =server> 我有以下Javascript代码可以在两张图片之间快速切换。 ;
< title>主页< / title>
< script src =Resources / jQuery.jstype =text / javascript>< / script>
< script type =text / javascript>
function changeImage()
{
requestAnimationFrame(changeImage);
var url = document.getElementById('Change_Image')。src;
$ b $ if(url =='Resources / Share1.bmp')
{
document.getElementById('Change_Image')。src ='Resources / Share2.bmp';
}
else
{
if(url =='Resources / Share2.bmp')
{
document.getElementById(' Change_Image')。src ='Resources / Share1.bmp';
}
}
}
< / script>
< / head>
< body>
< form id =form1runat =server>
< div>
< h1>欢迎使用我的网站< / h1>
< h2>下面你可以找到一个视觉加密的例子< / h2>
< br />
< br />
< div>< img id =Change_Imagesrc =Resources / Share1.bmpalt =Letter A/>< / div>
< / div>
< / form>
< / body>
< / html>
不幸的是,代码无法正常工作,图像也不会更改为另一个。我究竟做错了什么?我对JavaScript很陌生,请耐心等待?
您正在使用赋值运算符 比较运算符。在第二种情况下,还可以使用 else if
或更改为
if(url =='Resources / Share1.bmp')
和
else if(url =='Resources / Share2.bmp')
它应该可以工作。
请参阅此 DEMO 来帮助您。它以2秒的间隔切换图像
I have the following Javascript code which should rapidly switch between two images:
<head runat="server">
<title>Home Page</title>
<script src="Resources/jQuery.js" type="text/javascript"></script>
<script type="text/javascript">
function changeImage()
{
requestAnimationFrame(changeImage);
var url = document.getElementById('Change_Image').src;
if (url == 'Resources/Share1.bmp')
{
document.getElementById('Change_Image').src = 'Resources/Share2.bmp';
}
else
{
if (url == 'Resources/Share2.bmp')
{
document.getElementById('Change_Image').src = 'Resources/Share1.bmp';
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<h1>Welcome to my Website</h1>
<h2>Below you can find an example of visual cryptography</h2>
<br />
<br />
<div><img id="Change_Image" src="Resources/Share1.bmp" alt="Letter A" /></div>
</div>
</form>
</body>
</html>
Unfortunately, the code does not work and the image does not change to another one. What am I doing wrong? I am quite new to JavaScript so bear with me please?
You are using assign operator instead of comparison operator. Also use else if
or just else
in the second condition.
Change to
if (url == 'Resources/Share1.bmp')
and
else if (url == 'Resources/Share2.bmp')
and it should work.
See this DEMO to help you with. It toggles the image with 2 seconds interval
这篇关于Javascript - 在两个图像之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!