多个JavaScript按钮(变量)

多个JavaScript按钮(变量),javascript,html,Javascript,Html,我刚刚开始使用JavaScript,我想知道如何让不同的按钮做不同的事情。到目前为止,我可以让一个按钮做一件事,但如何让第二个按钮做另一件事呢?下面是代码: <html> <head> <script type="text/javascript"> function show_prompt() { var name=prompt("Your Name"); if (name!=null && name!="") { alert("Tha

我刚刚开始使用JavaScript,我想知道如何让不同的按钮做不同的事情。到目前为止,我可以让一个按钮做一件事,但如何让第二个按钮做另一件事呢?下面是代码:

<html>
<head>
<script type="text/javascript">
function show_prompt()
{
var name=prompt("Your Name");
if (name!=null && name!="")
  {
  alert("Thanks for clicking " + name + "!");
  window.top.location.replace("http://www.google.com");
  }
}
</script>
</head>
<body>
<ul>
<li>
<input type="button" onclick="show_prompt()" value="Button One" />
</ul>
</body>
</html>

函数show_prompt()
{
var name=提示(“您的姓名”);
如果(name!=null&&name!=“”)
{
提醒(“感谢您点击“+name+”!”);
window.top.location.replace(“http://www.google.com");
}
}

定义另一个函数并将第二个按钮绑定到它

function alert_hi() {
    alert("Hi!");
}

<input type="button" onclick="alert_hi()" value="Button Two" />
功能警报\u hi(){
警惕(“嗨!”);
}

如果您对此感兴趣,我强烈推荐。

定义另一个函数并将第二个按钮绑定到它

function alert_hi() {
    alert("Hi!");
}

<input type="button" onclick="alert_hi()" value="Button Two" />
功能警报\u hi(){
警惕(“嗨!”);
}

如果你对此感兴趣,我强烈推荐。

让第二个按钮做某事与让第一个按钮做某事基本相同。它只有两个功能和两个按钮。我想这就是你要问的

<html>
    <head>
    <script type="text/javascript">
        function doSomething()
        {
           // Do something when button one is clicked
        }

        function doSomethingElse() 
        {
           // Do something else when button two is clicked
        }
    </script>
</head>
<body>
    <input type="button" onclick="doSomething()" value="Button One" />
    <input type="button" onclick="doSomethingElse()" value="Button Two" />
</body>
</html>

函数doSomething()
{
//单击按钮1时执行某些操作
}
函数doSomethingElse()
{
//单击按钮2时执行其他操作
}

让第二个按钮做某事与让第一个按钮做某事基本相同。它只有两个功能和两个按钮。我想这就是你要问的

<html>
    <head>
    <script type="text/javascript">
        function doSomething()
        {
           // Do something when button one is clicked
        }

        function doSomethingElse() 
        {
           // Do something else when button two is clicked
        }
    </script>
</head>
<body>
    <input type="button" onclick="doSomething()" value="Button One" />
    <input type="button" onclick="doSomethingElse()" value="Button Two" />
</body>
</html>

函数doSomething()
{
//单击按钮1时执行某些操作
}
函数doSomethingElse()
{
//单击按钮2时执行其他操作
}

如果你认真学习。。你可以读一下

在你的例子中

js

html


如果你认真学习。。你可以读一下

在你的例子中

js

html


我猜你的意思是喜欢用不同的按钮做不同的事情,但使用相同的功能:

JavaScript:

function myFunction(str) {
    if(str.length > 3){
        alert("big");
    }else{
        alert("small");
    }
}
<input type="button" onclick="myFunction('test');" value="Button 1" />
<input type="button" onclick="myFunction('hi');" value="Button 2" />
HTML:

function myFunction(str) {
    if(str.length > 3){
        alert("big");
    }else{
        alert("small");
    }
}
<input type="button" onclick="myFunction('test');" value="Button 1" />
<input type="button" onclick="myFunction('hi');" value="Button 2" />


如果我的假设是错误的,只需创建不同的功能,并将按钮的
onclick
替换为各自的功能

我猜您的意思是使用不同的按钮,但使用相同的功能来做不同的事情:

JavaScript:

function myFunction(str) {
    if(str.length > 3){
        alert("big");
    }else{
        alert("small");
    }
}
<input type="button" onclick="myFunction('test');" value="Button 1" />
<input type="button" onclick="myFunction('hi');" value="Button 2" />
HTML:

function myFunction(str) {
    if(str.length > 3){
        alert("big");
    }else{
        alert("small");
    }
}
<input type="button" onclick="myFunction('test');" value="Button 1" />
<input type="button" onclick="myFunction('hi');" value="Button 2" />


如果我的假设是错误的,只需创建不同的函数,并用它们各自的函数替换按钮的
onclick
,这实际上不起作用。有没有办法按照第一个按钮的格式插入另一个按钮?我需要两个按钮都是输入名称的提示,当我尝试函数alert_uu'name'()时,它不起作用。确定<代码>函数hey_you(){var user_name=prompt(“你好!你叫什么名字?”);警报(“你好,user_name);}这实际上不起作用。有没有办法按照第一个按钮的格式插入另一个按钮?我需要两个按钮都是输入名称的提示,当我尝试函数alert_uu'name'()时,它不起作用。确定<代码>函数hey_you(){var user_name=prompt(“你好!你叫什么名字?”);警报(“你好,user_name);}