先看下面HTML代码:

<html>
<body>
    <form name=\"myForm\">
      <input type=\"button\"
             value=\"Big Button\"
             name=\"myButton\">
    </form>
</body>
</html>

我们要获取button按钮所在表单的name属性,该如何实现呢?其实很简单,这要使用到js的document对象,具体实现代码如下:

<html>
<body>
    <form name=\"myForm\">
      <input type=\"button\" value=\"Big Button\" id=\"myButton\">
    </form>
    <  language=\"text/ \">
<!--
http://www.manongjc.com/article/1802.html
作者:码农教程
-->
    alert(document.getElementById(\"button1\").form.id);
    </ >
</body>
</html>

从上面代码可以看出,直接使用document.getElementById("button1").form.id即可获取button按钮所在表单的name属性。

收藏 打印