javascript - 多个 onclick 事件 - 文本输出
问题描述
我有带有 onclick 事件的代码,用于更改按钮颜色和停止按钮被点击两次。我还想补充一点,当单击按钮时,它会将文本/数据添加到表格列/行中。单击按钮 3,按钮 3 中的 id 数据输出到列/行。接下来单击按钮 1,将 id 数据添加到同一行/列,只需将其添加到列表顶部。
<!DOCTYPE html>
<html>
<head>
<script src="Https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$(".button").click(function () {
$(this).css('background', 'yellow');
});
});
</script>
<style>
.button {
background-color: #FFFFFF;
/* White */
border: 2px solid #f44336;
color: black;
padding: 10px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 50px;
border-radius: 50%;
width: 80px;
text-align: center;
cursor: pointer
}
.disabled {
background-color: #0000ff;
/* Blue */
color: white;
cursor: not-allowed;
}
.button-clicked {
background-color: #FFFF00;
/* Yellow */
}
.buttonreset {
background-color: #FF0000;
/* Red */
border: 4px solid #f44336;
color: black;
padding: 10px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
border-radius: 20%;
width: 180px;
text-align: center;
cursor: pointer
}
td:not(:first-child) {
padding-top: 5px;
padding-bottom: 5px;
padding-right: 5px;
}
</style>
</head>
<body bgcolor="black" text="white">
<table width="100%">
<tr>
<td align="center"><button class="button" id="B1" onclick="myFunction();
this.disabled = true">1</button></td>
<td align="center"><button class="button" id="B2" onclick="myFunction(); this.disabled = true">2</button></td>
<td align="center"><button class="button" id="B3" onclick="myFunction(); this.disabled = true">3</button></td>
<td>PUT ID DATA HERE IN ORDER OF BUTTON PRESSED
</td>
</tr>
</table><br><br><br><br>
</body>
</html>
解决方案
这是你想要的吗?你已经有一个点击功能,你只需要.append()
一个新的行到带有 id 的表。就像是$("tr").append("<td>" + $(this).attr('id') + "</td>");
同时onclick="myFunction(); this.disabled = true"
从您的 HTML 中删除它,因为它会引发错误。
工作片段:
<!DOCTYPE html>
<html>
<head>
<script src="Https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$(".button").click(function () {
$(this).css('background', 'yellow');
$("tr").append("<td>" + $(this).attr('id') + "</td>");
});
});
</script>
<style>
.button {
background-color: #FFFFFF;
/* White */
border: 2px solid #f44336;
color: black;
padding: 10px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 50px;
border-radius: 50%;
width: 80px;
text-align: center;
cursor: pointer
}
.disabled {
background-color: #0000ff;
/* Blue */
color: white;
cursor: not-allowed;
}
.button-clicked {
background-color: #FFFF00;
/* Yellow */
}
.buttonreset {
background-color: #FF0000;
/* Red */
border: 4px solid #f44336;
color: black;
padding: 10px 10px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 20px;
border-radius: 20%;
width: 180px;
text-align: center;
cursor: pointer
}
td:not(:first-child) {
padding-top: 5px;
padding-bottom: 5px;
padding-right: 5px;
}
</style>
</head>
<body bgcolor="black" text="white">
<table width="100%">
<tr>
<td align="center"><button class="button" id="B1">1</button></td>
<td align="center"><button class="button" id="B2">2</button></td>
<td align="center"><button class="button" id="B3">3</button></td>
</td>
</tr>
</table><br><br><br><br>
</body>
</html>
推荐阅读
- docker-compose - Hyperledger - MSP 错误:提供的身份无效:x509:证书由未知机构签名
- android - 从嵌套的回收器视图中获取数据
- here-olp - 如何调查在创建日志 URL 之前失败的失败管道?
- excel - 使用 Do Loop VBA 选择 SAP 字段
- listview - JavaFX对象listView将对象名称显示为列表中的tostring
- server - Apache Ignite 服务器到客户端的连接问题
- scala - 如何将 java.util.HashSet[java.lang.Long] 转换为 Scala Set[Long]
- c# - 使用 C# 插值时如何处理变量中的空格?
- debugging - Salesforce 调试日志 VFRemoting 过滤
- powerbi - Power BI DAX 公式一起除法和减法