javascript - 更改 "onclick"事件中的 HTML 按钮标题

标签 javascript html

这是我在一个非常简单的页面上的按钮的 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 Toggled”。显然,我想做更多,但我现在只是在调试。然而,我似乎不知道如何做这个简单的事情。

应该很简单

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 中修复了这个问题

(感谢 RobG 在下面指出这一点)

或者你可以使用textContent

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

关于javascript - 更改 "onclick"事件中的 HTML 按钮标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31865445/

相关文章:

javascript - var self = 这种模式的替代方案

javascript - Highcharts - 强制显示 x 轴中的类别,即使没有数据

javascript - AnnyangJS 的输入/输出数据库

jquery - 人工登录 - 使用 HTML 和 jQuery

jquery - 单击更改列表图像项

javascript - 如何在 180° 子午线附近显示传单标记?

javascript - 从中心向外辐射显示图像,而不是按行从左上角显示图像

javascript - 在新页面加载时保持滚动位置

html - 是否有必要将行包装在 Bootstrap 的行类中?

javascript - 为什么javascript中这个正则表达式的结果是真的?