在“onclick"”中更改HTML按钮标题事件
Change HTML button caption in "onclick" event
这是我在一个非常简单的页面上的一个按钮的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/
Javascriptfunction 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中打开了底层文件,它工作得很完美(经你们中的一些人修改....)
谢谢你的关注。
相关文章:
- 我的HTML按钮没有在Javascript中运行
- 将插件制作的Javascript包含到html按钮中
- 变量不递增如何I'我喜欢html按钮点击的时候
- 如何通过单击html按钮获得h1的文本值
- HTML 按钮点击函数无法使用 jQuery 变量作为参数
- 按下一个HTML按钮,该按钮使用一个功能在同一个新窗口中打开URL
- Html按钮点击事件未触发单选按钮
- 使用带HTML按钮的Knockout
- HTML按钮刷新页面而不是将数据发送到数据库
- 动态生成的html按钮无法加载页面
- 为什么不'这个HTML按钮不起作用
- HTML按钮在单击时表现得很奇怪
- HTML按钮获胜't更改JS变量
- 如何在HTML按钮中有一个静态onclick参数
- 自动点击html按钮
- 按时间启用HTML按钮
- 更改HTML按钮值的颜色,其中值来自调用Javascript的onclick
- 如何最好地编写重叠的 html 按钮
- HTML按钮的作用类似于从键盘按下的键,并在某些对象或整个网站上起作用
- HTML 按钮在 Javascript 的文本字段中添加数字