Использование jQuery gt () и lt () для доступа к диапазону некорректно работающих элементов - PullRequest
0 голосов
/ 04 марта 2020

Я пытаюсь поместить sh текст td элементов в массив для последующего использования. Я использую событие нажатия на кнопку, которая находится в последнем столбце элемента tr (я выделил IP-адрес).

screenshot

Я пытаюсь захватить каждый элемент, который больше 0 и меньше 7, однако моя консоль разработчика показывает, что последний все еще захвачен, что было бы кнопкой.

Это вывод консоли:

    Array Val: xx.xxx.xx.xxx
    Array Val: AT&T
    Array Val: some random text
  3 Array Val: <--- Notice the 3 here? That should the next 3 elements, which is fine
    Array Val: <--- THIS SHOULD NOT BE HERE SINCE IT WASN'T SUPPOSED TO GET PUSHED TO THE ARRAY

Вот мой код для создания этого массива:

//Click the button
$('#bAcep').on('click', function() {
    //Get the parent of the parent of the parent of the button, find the 'td' elements
    //between the first and the last (1 - 6).
    var text = $(this).parent().parent().parent().find('td:gt(0):lt(7)');
    //Now add each of those to the array
    text.each(function() {
        array.push($(this).text());
    });
    //Loop through array and print to console
    $.each(array, function(index, val) {
        console.log("Array Val: "+val);
    })
})

1 Ответ

0 голосов
/ 04 марта 2020

Изменение моего диапазона lt () / gt () исправило это. Я также добавил некоторый отладочный код, чтобы увидеть html каждого элемента, который я помещал в массив (часть outerHTML):

$('#bAcep').on('click', function() {
  var text = $(this).parent().parent().parent().find('td:gt(0):lt(6)'); //<-- Changed to 6 instead of 7
  text.each(function() {
    array.push($(this).text());
    console.log("outerHTML: "+$(this).get(0).outerHTML); //<-- Added
  });
$.each(array, function(index, val) {
  console.log("Array Index: " + index + ", Array Val: "+val);
 })
})

Это дало мне нужные мне нужные индексы:

Dev Console Screenshot

...