javascript - 如何更改“" 元素动态使用 Javascript?
问题描述
我正在开发一个应用程序,该应用程序左侧有 2 个表格,右侧有一行,当用户选择任何数字时,表格上的值会动态添加到右侧的行上。
这可以按预期工作,但我正在尝试添加 CSS,如果用户将鼠标悬停在行(右侧)的输入上,它会变为绿色,并且表格背景颜色上的相应值/td 会立即变为绿色。
这是我的尝试:
// window.onload = function () { alert("Js working!") };
let currentInput = 1;
let bonusInput = 1;
$("#table1").on('click', function(event) {
let num = event.target.textContent;
$("#inp" + currentInput++).attr("value", num);
});
// bonus input:
$("#table2").on('click', function(event) {
let bon = event.target.textContent;
$("#bonus" + bonusInput++).attr("value", bon);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<!--Table on the left -->
<div style="width: 140px; float: left;">
<table id="table1">
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
<!-- Rows on the right-->
<!--2nd table-->
<div style="width: 140px; float: left; margin-left: 12px;">
<table id="table2">
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
<!-- Rows on the right-->
<!-- Input values to populate dynamically-->
<div style="width: 600px; float: right;">
<div>
<input type="text" size="4" id="inp1" value="">
<input type="text" size="4" id="inp2" value="">
<input type="text" size="4" id="inp3" value="">
<input type="text" size="4" id="inp4" value="">
<input type="text" size="4" id="inp5" value=""> +
<input style="margin-left: 20px;" type="text" size="4" id="bonus1" value="">
</div>
<div style="margin-top: 5px;">
<input type="text" size="4" id="inp7" value="">
<input type="text" size="4" id="inp8" value="">
<input type="text" size="4" id="inp9" value="">
<input type="text" size="4" id="inp10" value="">
<input type="text" size="4" id="inp11" value=""> +
<input style="margin-left: 20px;" type="text" size="4" id="bonus2" value="">
</div>
</div>
这是结果的屏幕截图:
解决方案
您可以改用 JavaScript:
// window.onload = function () { alert("Js working!") };
let currentInput = 1;
let bonusInput = 1;
$("#table1").on('click', function(event) {
let num = event.target.textContent;
$("#inp" + currentInput++).attr("value", num);
});
//Bonus input
$("#table2").on('click', function(event) {
let bon = event.target.textContent;
$("#bonus" + bonusInput++).attr("value", bon);
});
$('#result input').hover(function() {
var key = parseInt(this.id.replace(/(inp|bonus)/gm, ''), 10);
var dir = this.id.indexOf('bonus') >= 0 ? '1' : '2';
var row = (key > 6 || this.id == 'bonus2') ? 1 : 0;
var col = key % 6;
$('td').attr('style', '');
$('input').attr('style', '');
var table = '1';
if (dir == 1) table = '2';
var compare_key = this.value;
$('#table' + table + ' td').each(function() {
if (this.innerText.trim() == compare_key) {
$(this).css('background-color', 'green').css('color', 'white');
}
});
$(this).css('background-color', 'green').css('color', 'white');
});
* {
font-size: 15px;
}
td {
text-align: center;
border: 2px solid red;
line-height: 3.5rem;
width: 200px;
cursor: pointer;
}
td:hover {
background-color: green;
color: white;
}
#table1 td {}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Draggable Bootstrap nav-tabs with jQuery UI</title>
<meta name="description" content="Draggable Bootstrap nav-tabs with jQuery UI">
<!-- include bootstrap -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>
<body>
<!--Table on the left -->
<div style="width: 140px; float: left;">
<table id="table1">
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
<!-- Rows on the right-->
<!--2nd table-->
<div style="width: 140px; float: left; margin-left: 12px;">
<table id="table2">
<tbody>
<tr>
<td>1</td>
<td>2</td>
</tr>
<tr>
<td>3</td>
<td>4</td>
</tr>
<tr>
<td>5</td>
<td>6</td>
</tr>
<tr>
<td>7</td>
<td>8</td>
</tr>
<tr>
<td>9</td>
<td>10</td>
</tr>
</tbody>
</table>
</div>
<!-- Rows on the right-->
<!-- Input values to populate dynamically-->
<div id="result" style="width: 600px; float: right;">
<div>
<input type="text" size="4" id="inp1" value="">
<input type="text" size="4" id="inp2" value="">
<input type="text" size="4" id="inp3" value="">
<input type="text" size="4" id="inp4" value="">
<input type="text" size="4" id="inp5" value=""> +
<input style="margin-left: 20px;" type="text" size="4" id="bonus1" value="">
</div>
<div style="margin-top: 5px;">
<input type="text" size="4" id="inp7" value="">
<input type="text" size="4" id="inp8" value="">
<input type="text" size="4" id="inp9" value="">
<input type="text" size="4" id="inp10" value="">
<input type="text" size="4" id="inp11" value=""> +
<input style="margin-left: 20px;" type="text" size="4" id="bonus2" value="">
</div>
</div>
<!-- include jQuery -->
<script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
<!-- include jQuery UI -->
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script>
<!-- include bootstrap -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
</body>
</html>
推荐阅读
- javascript - 将键和值转换为字符串 javascript
- django - S3 Lambda 触发器不会为每个文件上传触发
- java - 将子模块发布到本地 maven
- reactjs - 使用反应钩子形式控制器注册 react-bootstrap-typeahead
- electron - 内容安全策略通配符似乎被忽略了
- excel - 为什么vba有时找不到工作簿
- amazon-web-services - CodeBuild 显示成功;但是,使用 Packer 时应该说 FAILED
- r - 与计数分组后不能使用更宽的枢轴
- rust - 无法在 android 上接收板/多播消息
- sql - 查询返回当前订单前后特定Product ID的订单数量