getElementbyId 的问题

发布于 2024-08-09 19:05:37 字数 788 浏览 9 评论 0原文

我在 getElementbyId 方面遇到问题,如下所示:

<script type="text/javascript">
<!--
function show_links(locale) {

        var box = document.getElementById(locale);

        if(box.style.display == "none") { 
            box.style.display = "inline";
        } 
         else { 

            box.style.display = "none";
        }

    }
    //-->
</script>

<div class="link"><strong><a href="javascript:show_links(test);">Test</a></strong></div>
<div class="test"> Blah blah blah. This content comes and goes. </div>

所以您有代码。当我单击链接“测试”时,它应该隐藏“等等等等文本”。再次单击时,它应该显示。但是,我有一个奇怪的问题。我通过调试器处理了代码,似乎行 var box = document.getElementById(locale); 无法正常工作。 box 被设置为 null。任何人都可以推理为什么吗?

I'm having a problem with getElementbyId as follows:

<script type="text/javascript">
<!--
function show_links(locale) {

        var box = document.getElementById(locale);

        if(box.style.display == "none") { 
            box.style.display = "inline";
        } 
         else { 

            box.style.display = "none";
        }

    }
    //-->
</script>

<div class="link"><strong><a href="javascript:show_links(test);">Test</a></strong></div>
<div class="test"> Blah blah blah. This content comes and goes. </div>

So there you have the code. When I click the link "Test", it should hide the "blah blah blah text". When clicked again, it should show. However, I have an odd problem. I processed the code through a debugger, and it seems that the line var box = document.getElementById(locale); is not working correctly. box is being set to null. Can anyone theorize why?

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

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

发布评论

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

评论(2

云巢 2024-08-16 19:05:37

你有几个问题。首先是关键的:

  1. 您传递给 show_links 函数的值是变量 test。这等于 undefined 因此它不会匹配任何内容。
  2. 您尝试通过 id 查找的元素没有 id。它只有一个类。

您需要为尝试匹配的元素提供 id 并传递字符串而不是未定义的值。

然后是较小的问题。

  1. 您正在测试内联显示样式属性,但默认情况下并未设置它。根据经验,最好调整 className 属性,并在样式表中定义样式。
  2. 您使用的是 javascript 伪 URI,而不是渐进增强
  3. 您有脚本内部包含的注释。在最好的情况下,这些都是毫无意义的。
  4. 您正在 inlinenone 之间摆弄 div,但 div 的默认显示值为 block。有理由将 div 样式设置为内联,但大多数时候您应该使用另一个元素。

You have several problems. First the critical ones:

  1. The value you are passing to the show_links function is the variable test. This is equal to undefined so it isn't going to match anything.
  2. The element you are trying to find by its id doesn't have an id. It has only a class.

You need to give the element you are trying to match an id and pass a string instead of an undefined value.

Then the lesser issues.

  1. You are testing the inline display style property, but you aren't setting it by default. As a rule of thumb, it is better to twiddle the className property, and have your styles defined in a stylesheet.
  2. You are using a javascript pseudo-URI instead of progressive enhancement.
  3. You have comments wrapped around the inside of the script. At best, these are pointless.
  4. You are twiddling a div between inline and none, but the default display value for a div is block. There are reasons to have a div styled inline, but most of the time you should be using another element.
朮生 2024-08-16 19:05:37
javascript:show_links(test);

test 是一个未知标识符。你在哪里定义的?你的意思是喂一根绳子吗?

<div class="test"> Blah blah blah. This content comes and goes. </div>

此行中没有 id 为 test 的元素。使用正确的属性id

javascript:show_links(test);

test is an unknown identifer. Where did you define it? Did you mean to feed a string?

<div class="test"> Blah blah blah. This content comes and goes. </div>

There is no element with an id of test in this line. Use the proper attribute id.

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