作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有 10 行数字,每行有 5 个数字,用点分隔,如下所示:
所有这些都在具有相同类名的行中。
我现在必须在每一行中查找它们加起来是否等于 90。
因此,如果第一行包含“2”和“88”(以任何顺序),它们就会突出显示(数字,而不是行)。与第二行、第三行等相同。
目的是找到 2 个数字,总和为 90(只能是两个,而不是 3 个或更多)。
我目前处于此状态:
function CheckAndHighlight(className){
var numebrsTd = $("td[class*="+className+"]");
var numbers = numebrsTd.html().split('.');
for(var i=0;i<numbers.length;i++)
{
var numI=parseInt(numbers[i],10);
for(var j=0;j<numbers.length;j++)
{
var numJ=parseInt(numbers[j],10);
// Avoid summing up numbers at same index
if(i==j) continue;
// Check if two numbers sum up to 90
if(numI+numJ == 90)
{
// Clear the row
numebrsTd.html('');
// Loop through all IP numbers of the row
for(var k=0;k<numbers.length;k++)
{
var numK=parseInt(numbers[k],10);
// Create span with th number
var span = $('<span>'+numK+'</span>')
// Check the index with index of numbers to be highlighted
var highlight = k == i || k == j;
// Add class to the span if to highlight
if(highlight) span.addClass('highlight');
// add '.' at the end of number if not last one
if(k<numbers.length) span.append('.');
// Add new Span to the row
numebrsTd.append(span);
}
return;
}
}
}
}
查看当前工作:http://lt.mrweb.info/
我需要通过调用该函数来使其工作 CheckAndHighlight('bari')
或CheckAndHighlight('cagliari')
最佳答案
试试这个。
以下代码的工作原理如下:
function ProcessRowsWithClass(className)
{
$('td[class*='+className+']').each(function(){
CheckAndHighlight($(this))
});
}
function CheckAndHighlight(numebrsTd)
{
var numbers = numebrsTd.html().split('.');
for(var i=0;i<numbers.length;i++)
{
var numI=parseInt(numbers[i],10);
for(var j=0;j<numbers.length;j++)
{
var numJ=parseInt(numbers[j],10);
// Avoid summing up numbers at same index
if(i==j) continue;
// Check if two numbers sum up to 90
if(numI+numJ == 90)
{
// Clear the row
numebrsTd.html('');
// Loop through all IP numbers of the row
for(var k=0;k<numbers.length;k++)
{
var numK=parseInt(numbers[k],10);
// Create span with th number
var span = $('<span>'+numK+'</span>')
// Check the index with index of numbers to be highlighted
var highlight = k == i || k == j;
// Add class to the span if to highlight
if(highlight) span.addClass('highlight');
// add '.' at the end of number if not last one
if(k<numbers.length) span.append('.');
// Add new Span to the row
numebrsTd.append(span);
}
return;
}
}
}
}
ProcessRowsWithClass('genova');
ProcessRowsWithClass('bari');
ProcessRowsWithClass('cagliari');
span.highlight{
color:green;
font-weight:bold;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<h1>Bari</h1>
<table>
<tr><td class="bari1">80.10.86.30.65</td></tr>
<tr><td class="bari2">96.11.73.36.13</td></tr>
<tr><td class="bari3">78.34.50.72.40</td></tr>
<tr><td class="bari4">34.78.69.60.22</td></tr>
<tr><td class="bari5">12.29.30.69.33</td></tr>
<tr><td class="bari6">70.10.20.70.44</td></tr>
</table>
<h1>Cagliari</h1>
<table>
<tr><td class="cagliari1">80.10.86.30.65</td></tr>
<tr><td class="cagliari2">96.11.73.36.13</td></tr>
<tr><td class="cagliari3">78.34.50.72.40</td></tr>
<tr><td class="cagliari4">34.78.69.60.22</td></tr>
<tr><td class="cagliari5">12.29.30.69.33</td></tr>
<tr><td class="cagliari6">70.10.20.70.44</td></tr>
</table>
<h1>Genova</h1>
<table>
<tr><td class="genova1">80.10.86.30.65</td></tr>
<tr><td class="genova2">96.11.73.36.13</td></tr>
<tr><td class="genova3">78.34.50.72.40</td></tr>
<tr><td class="genova4">34.78.69.60.22</td></tr>
<tr><td class="genova5">12.29.30.69.33</td></tr>
<tr><td class="genova6">70.10.20.70.44</td></tr>
</table>
关于javascript - [jQuery/javascript] : find a couple of items (2) in an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30951542/
我是一名优秀的程序员,十分优秀!