javascript 和 DHTML 影响

发布于 2024-11-02 02:54:52 字数 660 浏览 5 评论 0原文

我已经有一段时间没有使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

乱了心跳 2024-11-09 02:54:52

试试这个:http://jsfiddle.net/maniator/CEsJR/

<script type="text/javascript">
    var i =0;
var colors = ["red", "yellow", "blue", "green"];

function changeBorder(e)
{
    //restarts color counter
    i = i % colors.length;

    e.style.border = colors[i] + " solid 5px"
    i++;

    var timer = setTimeout(function(){changeBorder(e)}, 1000);
}

window.onload=function()
{


    var e = document.getElementById("text");

    e.style.border = "solid black 5px";
    e.style.padding = "5px";

    changeBorder(e);
}

</script>



<div id="text">
   <h1>Hello world</h1>I am Mike!.
</div>

e 变量只能在页面加载时创建

try this: http://jsfiddle.net/maniator/CEsJR/

<script type="text/javascript">
    var i =0;
var colors = ["red", "yellow", "blue", "green"];

function changeBorder(e)
{
    //restarts color counter
    i = i % colors.length;

    e.style.border = colors[i] + " solid 5px"
    i++;

    var timer = setTimeout(function(){changeBorder(e)}, 1000);
}

window.onload=function()
{


    var e = document.getElementById("text");

    e.style.border = "solid black 5px";
    e.style.padding = "5px";

    changeBorder(e);
}

</script>



<div id="text">
   <h1>Hello world</h1>I am Mike!.
</div>

the e variable can only be created on page load

蓝梦月影 2024-11-09 02:54:52

您可能想要:

e.style.borderColor = colors[i];

那么您需要担心 i 变得大于颜色的长度。

像这样的东西:

if (i >= colors.length) i=0;

可能是要走的路。

You probably want:

e.style.borderColor = colors[i];

then you'll need to worry about i becoming greater than the length of colors.

Something like:

if (i >= colors.length) i=0;

is probably the way to go.

凝望流年 2024-11-09 02:54:52

可能的问题#1

如果这是页面上所有内容运行的顺序,则 e = document.getElementById("text") 将永远无法工作。您只能在 HTML 构建元素后通过其 ID 来获取该元素。尼尔建议仅在页面加载时获取元素是可行的。或者,您可以将脚本移至页面底部。

可能的问题#2

在您的 changeBorder 函数中,您像这样应用新颜色:

e.border = colors[i];

但是您可能想要访问 e.style.border。即使您这样做了,我的理解是 border 是快捷方式样式,您可以在一个规则中定义宽度、样式和颜色。您可能想要使用诸如e.style.borderColor之类的东西。

可能的问题#3

看起来您还没有走得足够远,不足以成为一个问题,但请记住,当您循环使用不同的颜色时,最终您的 i 变量会更大比颜色数组的长度。为了确保这种情况永远不会发生,您可以使用模运算符:

e.style.borderColor = colors[i];
i = (i + 1) % 4;

Bonus

当您只为 setTimeout 提供一个不带参数的函数时,则不需要使用引号。以下内容很好,不需要 eval 函数:

setTimeout(changeBorder, 1000);

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:

e.border = colors[i];

But you probably meant to access e.style.border. And even if you did, my understanding is that border is the shortcut style, wherein you define the width, style, and color all in one rule. You'll probably want to use something like e.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:

e.style.borderColor = colors[i];
i = (i + 1) % 4;

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 the eval function:

setTimeout(changeBorder, 1000);
违心° 2024-11-09 02:54:52

这是一个示例。在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文