嗨,朋友们,当我点击另一个div时,我想在div中淡出,并为此使用以下代码。Code1工作正常,但我需要使用Code2。
我知道有jQuery,但我需要在JavaScript中这样做
你能告诉我我犯了什么错误或者我需要改变什么吗.
Code1
function starter() { fin(); }
function fin()
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + i + ")", i * 1000);
}
}
function seto(opa)
{
var ele = document.getElementById("div1");
ele.style.opacity = opa;
}Code2 --不起作用
function starter()
{
var ele = document.getElementById("div1");
fin(ele);
}
function fin(ele)
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout("seto(" + ele + "," + i + ")", i * 1000);
}
}
function seto(ele,opa)
{
ele.style.opacity = opa;
}发布于 2014-04-23 12:34:09
似乎您试图将元素转换为字符串。试一试这个
function starter()
{
var ele = document.getElementById("div1");
fin(ele);
}
function fin(ele)
{
for (i = 0; i <= 1; i += 0.01)
{
i=Math.round(i*100)/100;
setTimeout(function() { setto(ele,i); }, i * 1000);
}
}
function seto(ele,opa)
{
ele.style.opacity = opa;
}这里发生的是,当计时器命中时,我调用一个anonnymous函数,然后从这个函数中执行对setto的函数调用。
希望能帮上忙。乔纳斯
https://stackoverflow.com/questions/23244338
复制相似问题