无法通过 JavaScript 设置可见性:隐藏

发布于 2024-12-14 12:41:51 字数 1460 浏览 4 评论 0原文

我在理解这件事时遇到了一些困难,所以我认为将其发布在这里可能会有所帮助。所以就这样了。

我拥有的: 5 个不同的表格单元格中的 5 个不同图像 + 我将在下面发布的脚本。

我想要的:在预设时间之后使用javascript的...

document.GetElementById("image ID").style.visibility='visible/hidden'

...但是,不是图像ID,而是一个获取图像ID的字符串,并且在任何人说任何话之前,我没有使用“”作为 () 内部的字符串。类似......

var n=1;
function picID() {    
pictureID="pic"+n;
n=n+1;
}

这样,每次调用该函数时,我们都会获得“pic1”、“pic2”、“的元素 ID” pic3”等。

我的问题是:这该死的东西不起作用。图像样式与我在 img 标签中定义的相同。 (style="visibility:hidden")

所有图像 ID 都位于 img 标记内,这是理所应当的。

这是完整的代码:

<script>
var m=1;
        function Show() {
        if (m==6) {m=1;}

    feat="feat"+m;
        **document.getElementById(feat).style.visibility="visible";**
    m=m+1;
    setTimeout('Show()', 3000);
    }
window.onload = Show;
</script>

<script>
var k=1;    
    function Hide() {
    if (k==6) {k=1;}

    feate="feat"+k;
        **document.getElementById(feate).style.visibility="hidden";**
    k=k+1;
    setTimeout('Hide()', 3000);     
    }

window.onload = Hide;
</script>

我已经将代码分开,以便更容易发现。 据我所知,唯一的问题是代码中的粗体行。我已经通过将 document.getElementById 替换为 document.write 测试了其他所有内容,因此我可以看到自定义 ID 字符串工作正常。这是。还有其他一切。

有什么建议吗?谢谢。

I'm having a bit of trouble getting my head around this thing so I thought it might help to post it here. So here it goes.

What I have: 5 different images in 5 different table cells + a script that I will post below.

What I want: to use the javascript's...

document.GetElementById("image ID").style.visibility='visible/hidden'

...after a preset time, but, instead of image ID there is a string that gets the ID of the image, and before anyone says anything, I am not using "" for the string that is inside of (). Something like...

var n=1;
function picID() {    
pictureID="pic"+n;
n=n+1;
}

...and this way, every time that function is called we get the element ID of "pic1", "pic2", "pic3", etc.

What my problem is: the darn thing won't work. The image styling remains the same as I defined it in the img tag. (style="visibility:hidden")

All image ID's are within the img tag, as it should.

Here's the whole code:

<script>
var m=1;
        function Show() {
        if (m==6) {m=1;}

    feat="feat"+m;
        **document.getElementById(feat).style.visibility="visible";**
    m=m+1;
    setTimeout('Show()', 3000);
    }
window.onload = Show;
</script>

<script>
var k=1;    
    function Hide() {
    if (k==6) {k=1;}

    feate="feat"+k;
        **document.getElementById(feate).style.visibility="hidden";**
    k=k+1;
    setTimeout('Hide()', 3000);     
    }

window.onload = Hide;
</script>

I've separated the code so it'd easier to spot.
From what I've seen the only issue is the bold line within the code. I've tested everything else by replacing the document.getElementById with document.write, so I can see that the custom ID string thingy is working fine. It is. An everything else as well.

Any suggestions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

你另情深 2024-12-21 12:41:51

尝试设置 k = 0;而不是 k = 1;

看起来,当您加载页面时,show() 和 hide() 函数都在同一个元素上运行。执行上述操作将使 hide() 在 show() 运行的元素之前在该元素上运行。

Try setting k = 0; instead of k = 1;

It looks like when you load the page you have both the show() and the hide() functions running on the same element. Doing the above will make hide() run on the element BEFORE the element the show() runs on.

っ〆星空下的拥抱 2024-12-21 12:41:51

这段代码将继续循环运行。
因为setTimeOut()每次都会被调用。
应该存在不被调用的情况
例如,如果 m<6 则调用它,否则不调用

This code will keep on running in a loop.
because the setTimeOut() is called every time.
There should be a case when its not called
e.g if m<6 call it else don't

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