如何在IE中通过ID获取元素? (Visual Studio 8?)

时间:2010-01-21 20:49:41

标签: javascript dom internet-explorer

使用document.GetElementById(thingieID)似乎不起作用。 任何人都可以帮助这个DOM新手吗?

1 个答案:

答案 0 :(得分:1)

我不确定你要对元素做什么,或者问题是什么。

以下是在网页中使用document.GetElementById()的示例,该按钮在onClick事件上调用的函数中。

<html>
  <head>
    <title>Example of GetElementById</title>
  </head>
  <body>
    <p id="hello">Hello World</p>
    <input type="button" value="Update" onclick="btn1_Click();"/>
    <script type="text/javascript">
      // <![CDATA[
      function btn1_Click()
      {
          if (document.getElementById('hello')) {
              document.getElementById('hello').innerHTML = 'Hello World - this was inserted using JavaScript at ' + new Date().toString();
          }
      }
    // ]]>
    </script>
  </body>
</html>

你可以看到这个运行:
http://jsbin.com/afaga/2