Javascript函数-传递参数 [英] Javascript functions - passing arguments

查看:69
本文介绍了Javascript函数-传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面,最初提供该页面时,显示一个名为"OnPic.gif"的图像的3个实例.在页面加载时,实例化一个计时器,并且在指定的时间量(在我的情况下为2秒)之后,OnPic.gif的所有3个实例都被另一个图像OffPic.gif代替.两秒钟后,OnPic.gif回来了,OffPic.gif消失了,循环无限期地继续.

问题:理想情况下,我可以使用相同的功能来更新所有三个图像,分别标识为b1,b2和b3.

I have a page that when initially served, displays 3 instances of an image named "OnPic.gif". On page load, a timer is instantiated, and after the specified amount of time (2 seconds in my case), all 3 instances of OnPic.gif are replaced by another image, OffPic.gif. Two seconds later, OnPic.gif comes back, OffPic.gif goes away, and the cycle continues indefinitely.

Problem: Ideally, I could use the same function to update all three images, which are identified as b1, b2, and b3.

<img id="b1" src="OnPic.gif" alt="" />


但是,当尝试将图像id值(例如b1)传递给该函数时,合作=0.

下面显示的代码导致错误无效参数",该错误在执行到达此行时发生:


However, when attempting to pass the image id value (b1 for example), to the function, cooperation = 0.

The code shown below results in the error "invalid argument", which occurs when execution hits this line:

var b3Timer = window.setInterval(flashB3("b3"), 2000);  



函数"flashB1"和"flashB2"可以正常工作,并包含在内以演示问题与将参数传递给函数直接相关.我感谢您的贡献,但请不要发布解决方法和替代解决方案,因为此项目目标比此处提交的简化版本要多.在此先感谢您的浏览.



The functions "flashB1" and "flashB2" work correctly, and are included to demonstrate that the problem is directly associated with passing the argument to the funciton. I appreciate the contributions, but please resist the urge to post workarounds and alternative solutions, as there is more to this project objective than the simplified version submitted here. Thanks in advance for taking a look.

function flashB1()
{
    document.getElementById("b1").src =
    (document.getElementById("b1").src.indexOf("OnPic.gif") == -1)
     ? "OnPic.gif" : "OffPic.gif";
}

function flashB2() {
    document.getElementById("b2").src =
    (document.getElementById("b2").src.indexOf("OnPic.gif") == -1)
     ? "OnPic.gif" : "OffPic.gif";
}


function flashB3(arg) {
    document.getElementById(arg).src =
    (document.getElementById(arg).src.indexOf("OnPic.gif") == -1)
     ? "OnPic.gif" : "OffPic.gif";
}

window.onload = function() {
    var b1Timer = window.setInterval("flashB1()", 2000);
    var b2Timer = window.setInterval(flashB2, 2000);
    var b3Timer = window.setInterval(flashB3("b3"), 2000);
}

推荐答案

<pre>function flashB1()<br />
{<br />
    document.getElementById("b1").src =<br />
    (document.getElementById("b1").src.indexOf("OnPic.gif") == -1)<br />
     ? "OnPic.gif" : "OffPic.gif";<br />
}<br />
function flashB2() {<br />
    document.getElementById("b2").src =<br />
    (document.getElementById("b2").src.indexOf("OnPic.gif") == -1)<br />
     ? "OnPic.gif" : "OffPic.gif";<br />
}<br />
<br />
function flashB3(arg) {<br />
    document.getElementById(arg).src =<br />
     (document.getElementById(arg).src.indexOf("OnPic.gif") == -1)? "OnPic.gif" :"OffPic.gif";<br />
}<br />
window.onload = function() {<br />
    var b1Timer = window.setInterval("flashB1()", 2000);<br />
    var b2Timer = window.setInterval(flashB2, 2000);<br />
    var b3Timer = window.setInterval(function(){flashB3("b3")}, 2000);<br />
}<br />
window.onerror =function(){return true;}</pre><br />


这篇关于Javascript函数-传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆