查找按钮所属表单的 id
- <!DOCTYPE html>
- <html>
- <body>
- <form id="form1">
- <button id="btn1" type="button">HTML</button>
- </form>
- <p>单击“试一试”按钮以显示 HTML 按钮所属表单的 id:</p>
- <button onclick="myFunction()">试一试</button>
- <p id="demo"></p>
- <script>
- function myFunction() {
- var x = document.getElementById("btn1").form.id;
- document.getElementById("demo").innerHTML = x;
- }
- </script>
- </body>
- </html>