HTML 按钮点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18680248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:09:34  来源:igfitidea点击:

HTML button onclick event

javascripthtml

提问by bynu022

3 buttons 3 onclick events

3 个按钮 3 个 onclick 事件

This might be a very basic question; believe me I found very hard to find the answer to this question on the internet. I have 3 HTML pages stored in my server (Tomcat locally) & i want to open these HTML pages on a button click. Any help would be highly appreciated.

这可能是一个非常基本的问题;相信我,我发现很难在互联网上找到这个问题的答案。我有 3 个 HTML 页面存储在我的服务器(本地 Tomcat)中,我想通过单击按钮打开这些 HTML 页面。任何帮助将不胜感激。

Here is my code,

这是我的代码,

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

回答by Rahul Tripathi

Try this:-

尝试这个:-

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

回答by d1jhoni1b

You should all know this is inline scripting and is not a good practice at all...with that said you should definitively use javascript or jQuery for this type of thing:

你们都应该知道这是内联脚本,根本不是一个好习惯……也就是说,你应该明确地使用 javascript 或 jQuery 来处理这种类型的事情:

HTML

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

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

See comments why avoid inline scriptingand also why inline scripting is bad

查看评论为什么要避免内联脚本以及为什么内联脚本不好

回答by Mansoorkhan Cherupuzha

on first button add the following.

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

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

similarly do the rest 2 buttons.

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

回答by David Winiecki

Having trouble with a button onclick event in jsfiddle?

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

If so see Onclick event not firing on jsfiddle.net

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

回答by AmanS

This example will help you:

这个例子将帮助你:

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

You can open next page on same page by:

您可以通过以下方式在同一页面上打开下一页:

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

回答by Ramu Uncle

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