javascript 和 DHTML 影响
我已经有一段时间没有使用 JavaScript 了——在获得证书之后,我开始学习 Perl,并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可以非常简单地更改 div 的边框。然而,我无法弄清楚我哪里出错了。感谢任何意见/建议或想法。提前TY。这是我的脚本..
<script type="text/javascript">
var i =0;
var e = document.getElementById("text");
e.style.border = "solid black 5px";
e.style.padding = "5px";
var colors = ["red", "yellow", "blue", "green"];
function changeBorder()
{
e.border = colors[i];
i++;
var timer = setTimeout("changeBorder()", 1000);
}
window.onload=function()
{
changeBorder();
}
</script>
<div id="text">
<h1>Hello world</h1>I am Mike!.
</div>
It's been a while since I worked with JavaScript - after my cert I began learning Perl and have worked with it ever since. I am just trying to get my hand back in and for a start with JS, I have written this what I would have said, was a simple, little script to change a border of a div quite simply. I cannot work out however, just where I'm going wrong. Appreciated of any advice/suggestion or ideas. TY in advance. Here's my script..
<script type="text/javascript">
var i =0;
var e = document.getElementById("text");
e.style.border = "solid black 5px";
e.style.padding = "5px";
var colors = ["red", "yellow", "blue", "green"];
function changeBorder()
{
e.border = colors[i];
i++;
var timer = setTimeout("changeBorder()", 1000);
}
window.onload=function()
{
changeBorder();
}
</script>
<div id="text">
<h1>Hello world</h1>I am Mike!.
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:http://jsfiddle.net/maniator/CEsJR/
e 变量只能在页面加载时创建
try this: http://jsfiddle.net/maniator/CEsJR/
the
e
variable can only be created on page load您可能想要:
那么您需要担心 i 变得大于颜色的长度。
像这样的东西:
可能是要走的路。
You probably want:
then you'll need to worry about i becoming greater than the length of colors.
Something like:
is probably the way to go.
可能的问题#1
如果这是页面上所有内容运行的顺序,则
e = document.getElementById("text")
将永远无法工作。您只能在 HTML 构建元素后通过其 ID 来获取该元素。尼尔建议仅在页面加载时获取元素是可行的。或者,您可以将脚本移至页面底部。可能的问题#2
在您的
changeBorder
函数中,您像这样应用新颜色:但是您可能想要访问
e.style.border
。即使您这样做了,我的理解是border
是快捷方式样式,您可以在一个规则中定义宽度、样式和颜色。您可能想要使用诸如e.style.borderColor
之类的东西。可能的问题#3
看起来您还没有走得足够远,不足以成为一个问题,但请记住,当您循环使用不同的颜色时,最终您的
i
变量会更大比颜色数组的长度。为了确保这种情况永远不会发生,您可以使用模运算符:Bonus
当您只为
setTimeout
提供一个不带参数的函数时,则不需要使用引号。以下内容很好,不需要eval
函数:Possible Problem #1
If this is the order in which everything is run on your page, then
e = document.getElementById("text")
will never work. You can only get an element by its ID after it has been built by the HTML. Neal's suggestion of only getting the element on page load would work. Or, you could move your script to the bottom of the page.Possible Problem #2
In your
changeBorder
function, you apply the new color like so:But you probably meant to access
e.style.border
. And even if you did, my understanding is thatborder
is the shortcut style, wherein you define the width, style, and color all in one rule. You'll probably want to use something likee.style.borderColor
.Possible Problem #3
It doesn't look like you're getting far enough for this to be an issue, but keep in mind that as you cycle through the different colors, eventually your
i
variable will be bigger than the length of the color array. To make sure that never happens, you could use the modulus operator:Bonus
When you're only giving
setTimeout
a function, with no arguments, then you don't need to use quotes. The following is fine, and doesn't require theeval
function:这是一个示例。在 setTimeout 中,您可以只使用指向要执行的函数的指针。我添加了一种在 i = 4 后重新启动颜色开关的机制。
Here's an example. In setTimeout, you can just use a pointer to the function you want to execute. I've included a mechanism to restart the color switch after i = 4.