Быстрая и простая фильтрация элементов на странице с помощью jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
function show_only_proper_items() { var $items_all = $('.item'); var select_value = $('#select').val(); var $items_filtered_by_select_value = $items_all.filter('[data-for-select="'+select_value+'"]'); var filter_text = $('#text').val(); if (filter_text == '') { var $items_filtered_by_text = $items_filtered_by_select_value; } else { var $items_filtered_by_name = $items_filtered_by_select_value.filter('[data-item-name*="'+filter_text+'"]'); var $items_filtered_by_description = $items_filtered_by_select_value.filter('[data-item-description*="'+filter_text+'"]'); var $items_filtered_by_text = $items_filtered_by_name.add($items_filtered_by_description); } var $items_to_show = $items_filtered_by_text; var $items_to_hide = $items_all.not($items_to_show); $items_to_hide.hide(); $items_to_show.show(); } |
Вот такой простой и понятный код.
Работает быстро, потому что мы работали с DOM’ом только в самом начале и в конце. Плюс мы применяем фильтры к всё более и более маленькой выборке.
Не мелькает, потому что мы прячем только лишние элементы. Но в то же время мы не сначала показываем нужные, потом скрываем ненужные — чтобы это занимало меньше памяти и страница не разрасталась, пусть даже в моменте.
Применять этот метод можно например вот так:
1 2 3 4 5 |
jQuery(function($) { $('#select').on('change', show_only_proper_items); $('#text').on('input', show_only_proper_items); show_only_proper_items(); }); |
Пожалуйста!
Обсуждение
Great content! Super high-quality! Keep it up! :)
Great article! I really appreciate the clear and detailed insights you’ve provided on this topic. It’s always refreshing to read content that breaks things down so well, making it easy for readers to grasp even complex ideas. I also found the practical tips you’ve shared to be very helpful. Looking forward to more informative posts like this! Keep up the good work!
https://cr-v.su/forums/index.php?autocom=gallery&req=si&img=3950
Very good https://is.gd/tpjNyL
Good https://is.gd/tpjNyL
You don’t have to worry about long waiting times, this video chat matches you instantly.
Un site moderne et sécurisé: visitez ici.
You can use Uhmegle text chat on mobile easily.
Sehr empfehlenswert: hier klicken.
Video chat without signup, go to Uhmegle video.
Great article on best Omegle alternatives 2025.
It’s a fresh site compared to Omegle, join via Uhmegle.org.
https://shorturl.fm/i7aHM
https://shorturl.fm/ErXCU