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

时间:2015-08-06 20:43:29

标签: 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>

4 个答案:

答案 0 :(得分:1)

基本上你需要定位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";
}

答案 1 :(得分:1)

首先,您需要正确关闭元素。 试试这个..

RichTextBox1.Text = My.Computer.Clipboard.GetText(TextDataFormat.Rtf)

然后如果你想。将'script'放在.js文件中

答案 2 :(得分:0)

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

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

答案 3 :(得分:0)

谢谢,谢谢你的帮助。事实证明,CompoZer中的预览浏览器是个问题。它根本不会执行JS。我在Chrome中打开了底层文件,它运行得很好(经过你们几个人的修改......)

我感谢你的关注。我非常感谢它。