【发布时间】:2019-05-24 16:09:34
【问题描述】:
从这个链接jsfiddle.net/facgwbsm复制的代码片段
我有一个应用程序,当用户单击 添加新项目按钮 时,动态添加行,效果很好。当点击表格上的任何数字时,它会在行中动态填充。当我在第一行上方时,背景颜色变为绿色,包括工作正常的桌子上的相应匹配。
我希望当其他行悬停在第一行的效果上时适用于后续行,其中整行的背景颜色变为绿色,表格上的相应输入..
//Code to add child and input fields dynamically
// Starting number of inputs
let count = 5;
// Wrapper
const wrapper = document.querySelector('#wrapper');
document.querySelector('#btn').addEventListener('click', () => {
const container = document.createElement('div');
for (let i = 0; i < 5; i++) {
// Increment the count to ensure that it is unique
count++;
// Create your new `<input>` element
const input = document.createElement('input');
input.type = 'text';
input.name = count;
input.size = '4';
input.id = `inp${count}`;
container.appendChild(input);
// Optional: add empty whitespace after each child
container.append(document.createTextNode(' '));
}
wrapper.appendChild(container);
});
//END code
let currentInput = 1;
let bonusInput = 1;
$("#table1 td").on('click', function(event) {
//gets the number associated with the click
let num = $(this).text();
//set it to input's value attribute
$("#inp" + currentInput++).attr("value", num);
});
//Bonus input
$("#table2").on('click', function(event) {
let bon = event.target.textContent;
$("#bonus" + bonusInput++).attr("value", bon);
});
$("input").hover(function(event) {
//alert($('#selection1 input').serialize());
//let num = $(this).attr("value");
let parent = $(this).parent();
$(parent.children()).each(function(index, element) {
let num = $(element).val();
//console.log(num);
if (num) {
//Change input color on hover
$(this).css("backgroundColor", "green");
//Change tables bgcolor on hover
$("#table1 td").each(function() {
if ($(this).text() === num) $(this).css("backgroundColor", "green");
});
// $("#table2 td").each(function() {
// if ($(this).text() === num) $(this).css("backgroundColor","green");
// });
}
});
},
function(event) {
//Change input color on hover out
let parent = $(this).parent();
$(parent.children()).each(function(index, element) {
$(element).css("backgroundColor", "white");
});
//Change tables bgcolor on hover out
$("#table1 td").each(function() {
$(this).css("backgroundColor", "orange");
});
});
table {
border-collapse: collapse;
border: 5px solid black;
width: 100%;
}
td {
width: 50%;
height: 2em;
border: 1px solid #ccc;
background-color: orange;
text-align: center;
vertical-align: middle;
font-weight: bold;
cursor: pointer;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/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-->
<!-- Make sure each input has a unique id-->
<div style="width: 600px; float: right;">
<div id="wrapper">
<div>
<input type="text" name="1" size="4" id="inp1" value="">
<input type="text" name="2" size="4" id="inp2" value="">
<input type="text" name="3" size="4" id="inp3" value="">
<input type="text" name="4" size="4" id="inp4" value="">
<input type="text" name="5" size="4" id="inp5" value=""> +
<input style="margin-left: 20px;" type="text" name="6" size="4" id="bonus1" value="">
</div>
</div>
<button type="button" id="btn">Add new input group</button>
</div>
Javascript 代码
//Code to add child and input fields dynamically
// Starting number of inputs
let count = 5;
// Wrapper
const wrapper = document.querySelector('#wrapper');
document.querySelector('#btn').addEventListener('click', () => {
const container = document.createElement('div');
for (let i = 0; i < 5; i++) {
// Increment the count to ensure that it is unique
count++;
// Create your new `<input>` element
const input = document.createElement('input');
input.type = 'text';
input.name = count;
input.size = '4';
input.id = `inp${count}`;
container.appendChild(input);
// Optional: add empty whitespace after each child
container.append(document.createTextNode(' '));
}
wrapper.appendChild(container);
});
//END code
let currentInput = 1;
let bonusInput = 1;
$("#table1 td").on('click',function(event){
//gets the number associated with the click
let num = $(this).text();
//set it to input's value attribute
$("#inp" + currentInput++).attr("value",num);
});
//Bonus input
$("#table2").on('click',function(event){
let bon = event.target.textContent;
$("#bonus" + bonusInput++).attr("value",bon);
});
$("input").hover( function(event) {
//alert($('#selection1 input').serialize());
//let num = $(this).attr("value");
let parent = $(this).parent();
$(parent.children()).each(function (index, element) {
let num = $(element).val();
//console.log(num);
if (num) {
//Change input color on hover
$(this).css("backgroundColor","red");
//Change tables bgcolor on hover
$("#table1 td").each(function() {
if ($(this).text() === num) $(this).css("backgroundColor","green");
});
// $("#table2 td").each(function() {
// if ($(this).text() === num) $(this).css("backgroundColor","green");
// });
}
});
},
function(event) {
//Change input color on hover out
let parent = $(this).parent();
$(parent.children()).each(function (index, element) {
$(element).css("backgroundColor","white");
});
//Change tables bgcolor on hover out
$("#table1 td").each(function() {
$(this).css("backgroundColor","orange");
});
});
</script>
【问题讨论】:
标签: javascript jquery css