使用document.getElementById(thisDiv).style.left = howFarLeft;无法重置DIV的左侧值。 [英] can't reset the left value of a DIV using document.getElementById(thisDiv).style.left = howFarLeft;

查看:67
本文介绍了使用document.getElementById(thisDiv).style.left = howFarLeft;无法重置DIV的左侧值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉这个愚蠢的问题,但我是Javascript的新手。我写了这个

脚本,希望在页面上为一些div块设置动画。您可以在此处看到

页面: http:// www .keymedia.biz / demo.htm


谁能告诉我为什么这些DIV不会向左漂移,因为它们是

应该?


< script language =" javascript">

var allDivs = new array();

allDivs [] =" todd1";

allDivs [] =" todd2";

allDivs [] =" todd3";

allDivs [] =" todd4";

allDivs [] =" todd5";

allDivs [] =" todd6";

allDivs [] =" todd7";

allDivs [] =" todd8";

allDivs [] =" todd9";

allDivs [] =" todd10";

allDivs [] =" todd11";

allDivs [] =" todd12";

allDivs [] =" todd13";

allDivs [] =" todd14";

allDivs [] =" todd15&qu ot ;;

allDivs [] =" todd16";

allDivs [] =" todd17";

allDivs [] =" ; todd18" ;;

allDivs [] =" todd19";

allDivs [] =" todd20";

allDivs [] = " todd21";

allDivs [] =" todd22";

allDivs [] =" todd23";

allDivs [] =" todd24";

allDivs [] =" todd25";

allDivs [] =" todd26";

allDivs [ ] =" todd27";

allDivs [] =" todd28";

allDivs [] =" todd29";

allDivs [] =" todd30";

allDivs [] =" todd31";

allDivs [] =" todd32";

allDivs [] =" todd33";

allDivs [] =" todd34" ;;

allDivs [] =" todd35";

allDivs [] =" todd36";

allDivs [] =" todd37" ;;

allDivs [] =" todd38";

allDivs [] =" todd39";

allDivs [] =" todd40";


var thisDiv ="" ;;

var howFarLeft = 0;


函数moveDivs(){

for(i = 0;我<计数(allDivs); i ++){

thisDiv = allDivs [i];

document.getElementById(thisDiv).style.visibility =''visible'';

howFarLeft = document.getElementById(thisDiv).style.left;

if(thisDiv ==" todd1")howFarLeft = howFarLeft - 1;

if(thisDiv = =" todd2")howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd3")howFarLeft = howFarLeft - 1;

if(thisDiv ==" ; todd4")howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd5")howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd6" ;)howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd7")howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd8") howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd9")howFarLeft = howFarLeft - 1;

if(thisDiv ==" todd10")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd11")howFarLeft = howFarL eft - 2;

if(thisDiv ==" todd12")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd13")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd14")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd15")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd16")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd17")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd18")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd19")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd20")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd21")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd22")howFarLeft = howFarLeft - 2;

if(thisDiv ==" todd23")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd24")howFarLeft = howFarLeft - 3;

if(t hisDiv ==" todd25")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd26")howFarLeft = howFarLeft - 3;

if(thisDiv = =" todd27")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd28")howFarLeft = howFarLeft - 3;

if(thisDiv ==" ; todd29")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd30")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd31" ;)howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd32")howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd33") howFarLeft = howFarLeft - 3;

if(thisDiv ==" todd34")howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd35")howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd36")howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd37")howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd38&qu ot;)howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd39")howFarLeft = howFarLeft - 4;

if(thisDiv ==" todd40" )howFarLeft = howFarLeft - 4;

if(howFarLeft< -1500)howFarLeft = 9000;

document.getElementById(thisDiv).style.left = howFarLeft;

setTimeOut(" moveDivs()",100); < br $>
}

}


< / script>

解决方案
"劳伦斯" < LK ****** @ geocities.com>在消息中写道

news:da ************************** @ posting.google.c om

抱歉这个愚蠢的问题,但我是Javascript的新手。我写了这个
脚本,希望在页面上为一些div块设置动画。您可以在此处查看
页面: http://www.keymedia.biz /demo.htm

任何人都可以告诉我为什么这些DIV不会因为它们应该向左移动?


尝试更改此内容...

document.getElementById(thisDiv).style.left = howFarLeft;



....这个。


document.getElementById(thisDiv).style.left = howFarLeft +" px";


我不是专家,但我在自己的

页面上使用时发现上面的添加工作。


-

FZS600 - 银色/黑色

GS125 - 黑色/铁锈

福特100E Prefect - 黑色,自然

威士忌 - Aberlour Cask力量


2004年8月31日14:22:03 -0700,劳伦斯写道:

< script language ="的javascript">


< script type =''text / javascript''>

function divName(number){

return( ''todd''+ number);

}

var allDivs = new array();
allDivs [] =" todd1";
allDivs [] =" todd2" ;;


......依此类推......

allDivs [] =" todd40";




//取代了费力的方法。

for(int ii = 1; ii = 40; ii ++){

allDivs = divName(ii);

}


[在我更换太多之前,我会先走出JS

大师的路。

您的糟糕JS代码与我的(也是坏的)JS代码。 ]


......但是。


你是不是觉得脚本

失败了两个(或者是
usenet帖子的行)也将失败..两个

divs?


有关一些提示*修剪*损坏的代码

供其他人查看,请点击此处..

< http://www.physci.org/codes/sscce.jsp>


[它更适合Java,而不是JS,

但适用相同的原则。 ]


HTH


-

Andrew Thompson
http://www.PhySci.org/ 开源软件套件
http://www.PhySci.org/codes/ Web& IT帮助
http://www.1point1C.org/ Science&技术


allDivs [] =" todd1" ;;什么都不做。

你必须这样做:myArray [aNumber] =某事;

如果你不使用索引/数字javascript不知道如何处理

你的价值。


试试这个:


var allDivs = new Array();

for(var i = 0; i< 40; i ++){

allDivs [i] =" todd" +(i + 1);

}


使用myArray.length,不计算(myArray)。


另外,正如PDannyD指出的那样,在左边的值中添加''px'。


一般来说:尝试创建循环而不是编写大块代码。

Oeyvind


-
http://home.online.no/~oeyvtoft/ToftWeb/


Sorry for the dumb question but I''m new to Javascript. I wrote this
script hoping to animate some div blocks on a page. You can see the
page here: http://www.keymedia.biz/demo.htm

Can anyone tell me why these DIVs don''t drift to the left as they are
supposed to?

<script language="javascript">
var allDivs = new array();
allDivs[] = "todd1";
allDivs[] = "todd2";
allDivs[] = "todd3";
allDivs[] = "todd4";
allDivs[] = "todd5";
allDivs[] = "todd6";
allDivs[] = "todd7";
allDivs[] = "todd8";
allDivs[] = "todd9";
allDivs[] = "todd10";
allDivs[] = "todd11";
allDivs[] = "todd12";
allDivs[] = "todd13";
allDivs[] = "todd14";
allDivs[] = "todd15";
allDivs[] = "todd16";
allDivs[] = "todd17";
allDivs[] = "todd18";
allDivs[] = "todd19";
allDivs[] = "todd20";
allDivs[] = "todd21";
allDivs[] = "todd22";
allDivs[] = "todd23";
allDivs[] = "todd24";
allDivs[] = "todd25";
allDivs[] = "todd26";
allDivs[] = "todd27";
allDivs[] = "todd28";
allDivs[] = "todd29";
allDivs[] = "todd30";
allDivs[] = "todd31";
allDivs[] = "todd32";
allDivs[] = "todd33";
allDivs[] = "todd34";
allDivs[] = "todd35";
allDivs[] = "todd36";
allDivs[] = "todd37";
allDivs[] = "todd38";
allDivs[] = "todd39";
allDivs[] = "todd40";

var thisDiv = "";
var howFarLeft = 0;

function moveDivs() {
for (i=0; i < count(allDivs); i++) {
thisDiv = allDivs[i];
document.getElementById(thisDiv).style.visibility= ''visible'';
howFarLeft = document.getElementById(thisDiv).style.left;
if (thisDiv == "todd1") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd2") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd3") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd4") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd5") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd6") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd7") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd8") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd9") howFarLeft = howFarLeft - 1;
if (thisDiv == "todd10") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd11") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd12") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd13") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd14") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd15") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd16") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd17") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd18") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd19") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd20") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd21") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd22") howFarLeft = howFarLeft - 2;
if (thisDiv == "todd23") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd24") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd25") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd26") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd27") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd28") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd29") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd30") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd31") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd32") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd33") howFarLeft = howFarLeft - 3;
if (thisDiv == "todd34") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd35") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd36") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd37") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd38") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd39") howFarLeft = howFarLeft - 4;
if (thisDiv == "todd40") howFarLeft = howFarLeft - 4;
if (howFarLeft < -1500) howFarLeft = 9000;
document.getElementById(thisDiv).style.left = howFarLeft;
setTimeOut("moveDivs()", 100);
}
}

</script>

解决方案

"lawrence" <lk******@geocities.com> wrote in message
news:da**************************@posting.google.c om

Sorry for the dumb question but I''m new to Javascript. I wrote this
script hoping to animate some div blocks on a page. You can see the
page here: http://www.keymedia.biz/demo.htm

Can anyone tell me why these DIVs don''t drift to the left as they are
supposed to?
Try changing this...
document.getElementById(thisDiv).style.left = howFarLeft;



.... to this.

document.getElementById(thisDiv).style.left = howFarLeft + "px";

I''m no expert but I found the above addition to work when used on my own
pages.

--
FZS600 - Silver/Black
GS125 - Black/Rust
Ford 100E Prefect - Black, naturally
Whisky - Aberlour Cask Strength


On 31 Aug 2004 14:22:03 -0700, lawrence wrote:

<script language="javascript">
<script type=''text/javascript''>
function divName(number) {
return(''todd''+number);
}
var allDivs = new array();
allDivs[] = "todd1";
allDivs[] = "todd2";
...and so on, and so forth..
allDivs[] = "todd40";



// replaced laborious approach.
for (int ii=1; ii=40; ii++) {
allDivs = divName(ii);
}

[ Now I''ll step out of the way of the JS
gurus before I replace too much more of
your bad JS code with my (also bad) JS code. ]

...but.

Did it not occur to you that a script
that fails with 40 divs (or lines of
usenet post) will also fail for ..two
divs?

For some tips on *trimming* broken code
for others to look at, check here..
<http://www.physci.org/codes/sscce.jsp>

[ It is more geared to Java, than JS,
but the same principles apply. ]

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology


allDivs[] = "todd1"; doesnt do anything.
You would have to do: myArray[aNumber] = something;
If you dont use an index/a number javascript doesnt know what to do with
your value.

Try this:

var allDivs = new Array();
for(var i = 0; i < 40; i++){
allDivs[i] = "todd" + (i+1);
}

Use myArray.length, not count(myArray).

Also, as PDannyD noted, add ''px'' to the left values.

Generally: Try creating loops instead of writing big chunks of code.

Oeyvind

--
http://home.online.no/~oeyvtoft/ToftWeb/



这篇关于使用document.getElementById(thisDiv).style.left = howFarLeft;无法重置DIV的左侧值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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