簡體   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