繁体   English   中英

HTML按钮onclick事件

[英]HTML button onclick event

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>

试试这个:-

<!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>

你应该都知道这是内联脚本并且根本不是一个好习惯......据说你应该明确地使用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>

JQuery的

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

使用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';
  });

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

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

onclick="window.location.href='Students.html';"

同样做其余的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';">

这个例子可以帮助你:

<form>
    <input type="button" value="Open Window" onclick="window.open('http://www.google.com')">
</form>

您可以在同一页面上打开下一页:

<input type="button" value="Open Window" onclick="window.open('http://www.google.com','_self')">

jsfiddle中的按钮onclick事件有问题?

如果是这样,请参阅在jsfiddle.net上触发的Onclick事件

<body>
"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';">
</body>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM