在“onclick"”中更改HTML按钮标题事件

Change HTML button caption in "onclick" event

本文关键字:HTML 按钮 事件 标题 onclick quot      更新时间:2023-09-26

这是我在一个非常简单的页面上的一个按钮的HTML:

<button onclick="handleButton(this)" 
           name="buttonLeft"
           type="button" 
          class="myButton" 
              id="ButtonLeft">Toggle Left Garage</button>
    <br />Door is Secure</td>

在我的onclick事件处理程序(handleButton())中,我想将标题从"Toggle Left Garage"更改为"Left Garage Toggle"。显然,我想做更多,但我现在只是调试。然而,我似乎不知道如何做这件简单的事情。

应该像

一样简单
function handleButton(element) 
{
  element.something="Left Garage Toggled";
}

我试过element.value,它似乎不起作用。

好了,下面是没有修饰或空格修复的实际代码....

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <meta http-equiv="content-type"
 content="text/html; charset=ISO-8859-1">
  <title>Garage Doors</title>
  <style>
.myButton {
-moz-box-shadow: 0px 1px 0px 0px #97c4fe;
-webkit-box-shadow: 0px 1px 0px 0px #97c4fe;
box-shadow: 0px 1px 0px 0px #97c4fe;
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #3d94f6), color-stop(1, #1e62d0));
background:-moz-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-webkit-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-o-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:-ms-linear-gradient(top, #3d94f6 5%, #1e62d0 100%);
background:linear-gradient(to bottom, #3d94f6 5%, #1e62d0 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#3d94f6', endColorstr='#1e62d0',GradientType=0);
background-color:#3d94f6;
-moz-border-radius:6px;
-webkit-border-radius:6px;
border-radius:6px;
border:1px solid #337fed;
display:inline-block;
cursor:pointer;
color:#ffffff;
font-family:Arial;
font-size:19px;
font-weight:bold;
padding:6px 24px;
text-decoration:none;
text-shadow:0px 1px 0px #1570cd;
}
.myButton:hover {
background:-webkit-gradient(linear, left top, left bottom, color-stop(0.05, #1e62d0), color-stop(1, #3d94f6));
background:-moz-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-webkit-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-o-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:-ms-linear-gradient(top, #1e62d0 5%, #3d94f6 100%);
background:linear-gradient(to bottom, #1e62d0 5%, #3d94f6 100%);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#1e62d0', endColorstr='#3d94f6',GradientType=0);
background-color:#1e62d0;
}
.myButton:active {
position:relative;
top:1px;
}
  </style>
</head>
<body>
<table style="text-align: center; width: 681px; height: 381px;"
 border="1" cellpadding="2" cellspacing="2">
  <tbody>
    <tr>
      <td>
      <button onclick="handleButton(this)" name="buttonLeft"
 type="button" class="myButton" id="ButtonLeft">Toggle
Left Garage</button>
      <br '="">
Door is Secure</td>
      <td>
      <button onclick="handleButton(this)" name="buttonRight"
 class="myButton">Toggle Right
Garage
Door
      </button></td>
    </tr>
  </tbody>
</table>
<br>
<script>
function handleButton(caller)
{
caller.innerHTML=caller.id;
}
</script>
</body>
</html>

基本上你需要瞄准innerHTML。

jsFiddle: https://jsfiddle.net/grwovnyv/

Javascript

function handleButton(element) {
    element.innerHTML = "Left Garage Toggled";
}
HTML

<button onclick="handleButton(this)" name="buttonLeft" type="button" class="myButton" id="ButtonLeft">Toggle Left Garager</button>
<br/>Door is Secure

顺便说一下你的HTML有点乱但我在jsFiddle

中也修正了这个问题

(Thanks to RobG for pointing this out below)

或者你可以使用textContent

function handleButton(element) {
    element.textContent = "Left Garage Toggled";
}

首先你需要正确地关闭你的元素。试试这个. .

<button onclick="handleButton()"
    name="buttonLeft"
    type="button"
    class="myButton"
    id="ButtonLeft">
    Toggle Left Garage
</button>
<br />
Door is Secure
<script>
    function handleButton()
    {
        document.getElementById('ButtonLeft').innerHTML = "Left Garage Toggled";
    }
</script>

如果你想的话。将'script'放入.js文件

对于html我发现了一些错误!第5行应该像这样:

id="buttonleft">Toggle Left Garage</button>

谢谢大家的帮助。原来是comppozer中的预览浏览器出了问题。它似乎根本不执行JS。我在Chrome中打开了底层文件,它工作得很完美(经你们中的一些人修改....)

谢谢你的关注。