Быстрая и простая фильтрация элементов на странице с помощью 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
Интересный код! Очень люблю, когда всё просто и понятно. Кстати, если вам нужно следить за запасами товаров и получать профессиональные продукты напрямую от поставщиков, посмотрите на Suplery. Они делают это легко, и доставка бесплатная во многих случаях!
Yo, kuwingame11… it’s got a ring to it, doesn’t it? Gave it a spin and found some interesting stuff. The game selection’s pretty wide, definitely something for everyone. I’d recommend it if you’re looking for something new to try. Go give em’ a shot: kuwingame11.
PH88Game is pretty cool! They’ve got a good collection of games, something for everyone. Definitely worth a look! ph88game
777 casino seems like it will never let you down with the classic feel. Not bad at all! 777 casino
Welcome to S666.com! It is time to have some fun. I hope I get lucky today. Wanna join me? welcome to s666.com
I am thanksful for this post!
Plusph11, huh? New one to me. Gonna take a look around. The layout is pretty clean. Hope the odds are good. Wish me luck! plusph11
Really digging this breakdown of blackjack strategy! It’s cool how even setting up an account at a place like jiliko casino com requires a bit of ‘verification’ – feels secure! Mastering the basics is key, just like KYC!
playph https://www.playphbox.net
For all you casino game lovers, juegosdecasinos is your go-to spot. Lots of games to choose from! Get your game on: juegosdecasinos
Yo, Winforlife888login makes getting logged in a breeze. Quick and painless, so you can focus on winning! Seriously, no headaches here. winforlife888login
PHBest is pretty good. Nothing groundbreaking, but it gets the job done. What I like is its reliability. If you’re looking for something solid, give them a look. Browse at phbest.