HTML按钮onclick事件

byn*_*022 24 html javascript

3个按钮3点击事件

这可能是一个非常基本的问题; 相信我,我发现很难在互联网上找到这个问题的答案.我在我的服务器(本地Tomcat)中存储了3个HTML页面,我想在按钮点击时打开这些HTML页面.任何帮助将受到高度赞赏.

这是我的代码,

<!DOCTYPE html>
<html>
  <head>
      <meta charset="ISO-8859-1">
      <title>Online Student Portal</title>
  </head>
<body>
  <form action="">
      <h2>Select Your Choice..</h2>
      <input type="button" value="Add Students">
      <input type="button" value="Add Courses">
      <input type="button" value="Student Payments">
  </form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

Rah*_*thi 41

试试这个:-

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="ISO-8859-1"> 
    <title>Online Student Portal</title> 
</head> 
<body> 
    <form action="">
         <input type="button" value="Add Students" onclick="window.location.href='Students.html';"/>
         <input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"/>
         <input type="button" value="Student Payments" onclick="window.location.href='Payment.html';"/>
    </form> 
</body> 
</html>
Run Code Online (Sandbox Code Playgroud)

  • 出于用户体验的目的,这些输入实际上应该是链接,例如<a href="students.html">添加学生</a> (3认同)

d1j*_*i1b 25

你应该都知道这是内联脚本并且根本不是一个好习惯......据说你应该明确地使用javascript或jQuery来做这类事情:

HTML

<!DOCTYPE html> 
<html> 
 <head> 
    <meta charset="ISO-8859-1"> 
    <title>Online Student Portal</title> 
 </head> 
 <body> 
    <form action="">
         <input type="button" id="myButton" value="Add"/>
    </form> 
 </body> 
</html>
Run Code Online (Sandbox Code Playgroud)

JQuery的

var button_my_button = "#myButton";
$(button_my_button).click(function(){
 window.location.href='Students.html';
});
Run Code Online (Sandbox Code Playgroud)

使用Javascript

  //get a reference to the element
  var myBtn = document.getElementById('myButton');

  //add event listener
  myBtn.addEventListener('click', function(event) {
    window.location.href='Students.html';
  });
Run Code Online (Sandbox Code Playgroud)

请参阅注释,以避免内联脚本以及内联脚本编写错误的原因


Man*_*zha 6

在第一个按钮上添加以下内容.

onclick="window.location.href='Students.html';"
Run Code Online (Sandbox Code Playgroud)

同样做其余的2个按钮.

<input type="button" value="Add Students" onclick="window.location.href='Students.html';"> 
<input type="button" value="Add Courses" onclick="window.location.href='Courses.html';"> 
<input type="button" value="Student Payments" onclick="window.location.href='Payments.html';">
Run Code Online (Sandbox Code Playgroud)