使用Jquery进行条件突出显示

时间:2017-08-16 08:06:02

标签: javascript jquery css

在此示例https://jsfiddle.net/erv79u0w/中,单击“全选”时,表中将突出显示所有相应的值。

是否可以突出显示 ONLY 表中同一行中存在的值。例如,使用“全选”选择时的第一个值(A,B,X,Y),只应突出显示在同一行中的值。在这种情况下,它只是表格的第二行。因此,只应突出显示第二行的值。

  

同一行中的单元格不应突出显示,如下图所示。

enter image description here

$('.selector').each(function() {
  $(this).on('click', check);
});

$('.all').each(function() {
  $(this).on('click', all);
});

function all(event) {
  if ($(this).is(':checked')) {
    $("input:checkbox:not(:checked)", $(this).parents('form')).not(this).prop("checked", "checked");
  } else {
    $("input:checkbox(:checked)", $(this).parents('form')).not(this).prop("checked", "");
  }

  //$('.selector').prop("checked", this.name === "SelectAll");

  check(event);
}

function check(event) {
  var checked = $(".selector:checked").map(function() {
    return this.name
  }).get()
  $('td').removeClass("highlight").filter(function() {
    return $.inArray($(this).text(), checked) >= 0
  }).addClass("highlight")
  if ($(this).is(".selector"))
    $('.all').not(this).prop("checked", false)
}
.highlight {
  background: #9ac99d;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<div id="container">
  <div id="sidebar1">

      <div id="container">
        <div id="sidebar1">
          <h3>Parameters:</h3>
          <form id="form1" name="form1" method="post" action="">
            <label>
                <input type="checkbox" name="SelectAll" class="all" />Select All</label>
            <label>
                <input type="checkbox" name="A" class="selector" />A</label>
            <label>
                <input type="checkbox" name="B" class="selector" />B</label>
            <label>
                <input type="checkbox" name="X" class="selector" />X</label>
            <label>
                <input type="checkbox" name="Y" class="selector" />Y</label>
          </form>
          <form id="form2" name="form2" method="post" action="">
            <label>
                <input type="checkbox" name="SelectAll" class="all" />Select All</label>
            <label>
                <input type="checkbox" name="K" class="selector" />K</label>
            <label>
                <input type="checkbox" name="J" class="selector" />J</label>
            <label>
                <input type="checkbox" name="M" class="selector" />M</label>
            <label>
                <input type="checkbox" name="T" class="selector" />T</label>
          </form>
        </div>
      
      <div id="mainContent">
        <h3 align="right">&nbsp;</h3>
        <div>
          <table width="200" border="1">
            <tr>
              <td>A</td>
              <td>&nbsp;</td>
              <td>M</td>
              <td>&nbsp;</td>
              <td>K</td>
              <td>J</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>B</td>
              <td>A</td>
              <td>&nbsp;</td>
              <td>Y</td>
              <td>X</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>Y</td>
              <td>&nbsp;</td>
              <td>J</td>
              <td>T</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td> X</td>
              <td>&nbsp;</td>
              <td>X</td>
              <td>&nbsp;</td>
              <td>Y</td>
            </tr>
            <tr>
              <td>K</td>
              <td>Q</td>
              <td>&nbsp;</td>
              <td>T</td>
              <td>&nbsp;</td>
              <td>Y</td>
            </tr>
            <tr>
              <td>M</td>
              <td>&nbsp;</td>
              <td>T</td>
              <td>K</td>
              <td>J</td>
              <td>Z</td>
            </tr>
          </table>
        </div>
      </div>

1 个答案:

答案 0 :(得分:1)

Array.prototype.compare = function(testArr) {
  if (this.length != testArr.length) return false;
  for (var i = 0; i < testArr.length; i++) {
    if (this[i].compare) { //To test values in nested arrays
      if (!this[i].compare(testArr[i])) return false;
    } else if (this[i] !== testArr[i]) return false;
  }
  return true;
}
$(document).on('click', '.all', function() {
  $('#mainContent table tr td').removeClass('highlight');
  if (!$(this).is(':checked')) {
    $(this).closest('form').find('input.selector').prop('checked', false);
  }
  $('input[name=SelectAll]').each(function() {
    if ($(this).is(':checked')) {
      $(this).closest('form').find('input.selector').prop('checked', true);
      processAllClick($(this));
    } else {
      processIndividualClick($(this));
    }
  });
});
$(document).on('click', '.selector', function() {
  $('#mainContent table tr td').removeClass('highlight');
  if (!$(this).is(':checked')) {
    $(this).closest('form').find('input.all').prop('checked', false);
  }
  $('input[name=SelectAll]').each(function() {
    if ($(this).is(':checked')) {
      processAllClick($(this));
    } else {
      processIndividualClick($(this));
    }
  });
});

function processIndividualClick(input) {
  $(input).closest('form').find('input.selector:checked').each(function() {
    $('td:contains(' + $(this).attr('name') + ')').addClass('highlight');
  });
}

function processAllClick(input) {
  var elems = [];
  $(input).closest('form').find('input.selector:checked').each(function() {
    elems.push($(this).attr('name'));
  });
  $('#mainContent table tr').each(function() {
    var child = [];
    $(this).find('td').each(function() {
      if ($.inArray($(this).text(), elems) != -1)
        child.push($(this).text());
    });
    if (elems.sort().compare(child.sort())) {
      for (var i = 0; i < child.length; i++) {
        $(this).find('td').each(function() {
          if ($(this).text() == child[i]) {
            $(this).addClass('highlight');
          }
        });
      }
    }
  });
}
.highlight {
  background: #9ac99d;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="container">
  <div id="sidebar1">

    <div id="container">
      <div id="sidebar1">
        <h3>Parameters:</h3>
        <form id="form1" name="form1" method="post" action="">
          <label>
                <input type="checkbox" name="SelectAll" class="all" />Select All</label>
          <label>
                <input type="checkbox" name="A" class="selector" />A</label>
          <label>
                <input type="checkbox" name="B" class="selector" />B</label>
          <label>
                <input type="checkbox" name="X" class="selector" />X</label>
          <label>
                <input type="checkbox" name="Y" class="selector" />Y</label>
        </form>
        <form id="form2" name="form2" method="post" action="">
          <label>
                <input type="checkbox" name="SelectAll" class="all" />Select All</label>
          <label>
                <input type="checkbox" name="K" class="selector" />K</label>
          <label>
                <input type="checkbox" name="J" class="selector" />J</label>
          <label>
                <input type="checkbox" name="M" class="selector" />M</label>
          <label>
                <input type="checkbox" name="T" class="selector" />T</label>
        </form>
      </div>

      <div id="mainContent">
        <h3 align="right">&nbsp;</h3>
        <div>
          <table width="200" border="1">
            <tr>
              <td>A</td>
              <td>&nbsp;</td>
              <td>M</td>
              <td>&nbsp;</td>
              <td>K</td>
              <td>J</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>B</td>
              <td>A</td>
              <td>&nbsp;</td>
              <td>Y</td>
              <td>X</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td>Y</td>
              <td>&nbsp;</td>
              <td>J</td>
              <td>T</td>
              <td>&nbsp;</td>
            </tr>
            <tr>
              <td>&nbsp;</td>
              <td> X</td>
              <td>&nbsp;</td>
              <td>X</td>
              <td>&nbsp;</td>
              <td>Y</td>
            </tr>
            <tr>
              <td>K</td>
              <td>Q</td>
              <td>&nbsp;</td>
              <td>T</td>
              <td>&nbsp;</td>
              <td>Y</td>
            </tr>
            <tr>
              <td>M</td>
              <td>&nbsp;</td>
              <td>T</td>
              <td>K</td>
              <td>J</td>
              <td>Z</td>
            </tr>
          </table>
        </div>
      </div>

此处处理所有点击事件