1

Доброго времени суток! Скрипт должен скрыть строки таблицы с нулевыми остатками, остальное записать в словарь и сравнить цены (в таблице может быть несколько одинаковых позиций товара с разной ценой). То, что в этом словаре, нужно выделить цветом (построчно).

$(document).ready(function() {
  $("[id='thunder-button']").click(function() {
    var bestOfferDict = {}
    $(".hide-tr-class").each(function() {
      var nonZeroFound = false;
      var brand = (this).getElementsByTagName('td')[1].innerText;
      var number = (this).getElementsByTagName('td')[2].innerText;
      var price = (this).getElementsByTagName('td')[5].innerText;
      var rowId = $(this);
      $(this).find("td.thunder").each(function() {
        if ($(this).text().trim() != '0') {
          nonZeroFound = true;
          if (bestOfferDict[brand + ' ' + number]) {
            if (price < bestOfferDict[brand + ' ' + number].price) {
              bestOfferDict[brand + ' ' + number] = {
                'price': price,
                'rowId': rowId
              };
            }
          } else {
            bestOfferDict[brand + ' ' + number] = {
              'price': price,
              'rowId': rowId
            };
          }
        }
      });
      if (!nonZeroFound) {
        $(this).hide("slow");
      }
    });
    for (item in bestOfferDict) {
      item.rowId.setAttribute('style', 'background-color: green;');
    }
  })
})

Делает все, кроме выделения цветом. Что я не так делаю?

1 ответ 1

0

rowId - jQuery объект, а не DOMElement...

$().css:

bestOfferDict[item].rowId.css('background-color', 'green');

Или $().get:

bestOfferDict[item].rowId.get(0).style.backgroundColor = 'green';
4
  • спасибо за ответ, но в консоли - ошибка Uncaught TypeError: Cannot read property 'css' of undefined или Uncaught TypeError: Cannot read property 'get' of undefined
    – Sergii P.
    4 ноя 2017 в 20:58
  • а про setAttribute такой ошибки не было?
    – vp_arth
    4 ноя 2017 в 21:01
  • да, Uncaught TypeError: Cannot read property 'setAttribute' of undefined а не может быть проблема в var rowId = $(this);? пробую ставить console.log($(this)); сразу за var rowId = $(this); - сыплет в консоль обьектами, т.е. что-то в переменную-таки записывается...
    – Sergii P.
    4 ноя 2017 в 21:22
  • спасибо за наводку, @vp_arth немного поправил и всё встало на места!
    – Sergii P.
    5 ноя 2017 в 11:52

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.